📜  c# list any retun indec - C# 代码示例

📅  最后修改于: 2022-03-11 14:49:15.359000             🧑  作者: Mango

代码示例1
public class Item {
    public int Id { get; set; }
    public Item() {}
    public Item(int id) { Id = id; }
}
List idList = { new Item(1), new Item(2), new Item(3) };
Item[] idLArr = [ new Item(1), new Item(2), new Item(3) ];

Item newItem = new Item(1);

// Using a lambda expression we can do the following query
int index = idList.FindIndex(item => item.Id == newItem.Id);
int arrIndex = Array.IndexOf(idLArr, newItem.Id);

//Then use it like so:
if (index != -1)
{
    // The item exists at index 0!
}