📌  相关文章
📜  c# 值是否存在于列表中 - C# 代码示例

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

代码示例1
public int GetItemFromList() {
    List list = new List(
      new Item(1),
      new Item(2),
      new Item(3)
    );

    Item testItem = new Item(1);

    // Inside FindIndex() you can specify a lambda expression where you
    // query if an item exists like a boolean test.
    int index = list.FindIndex(item => testItem.Id == item.Id);

    // in this case out testItem.Id (1) is equal to an item in the list
    if (index > -1)
    {
        // We get here with the index 0!
          return index;
    }
}

public class Item
{
    public int Id { get; set; }
    public Item() { }
    public Item(int id)
    {
        Id = id;
    }
}