List.Find()用法

如果在一个 List内进行相同对象属性的过滤,用 .Contains 是能完成的。
这时候需要用到List.Find(Predicatematch)
这里就是泛型委托 举例
对象
Person public class Person
{
private int _id;
private string _name;
public int ID
{
   get{ return _id;}
   set{ _id = value;}
}
public int Name
{
  get{ return _name;}
  set{ _name= value;}
}
public Person(int id, string name)
{
  _id = id;
  _name = name;
}
}
这个对象里 有2个 属性,我们接下来创建一个Person 的 List 集合 ,然后用 Find去查找
 public void CreateAndSearchList()
 {
 //create and fill the collection
 List myList = new List();
 myList.Add(new Person(1, "AndreySanches"));
 myList.Add(new Person(2, "AlexandreTarifa"));
 myList.Add(new Person(3, "EmersonFacunte"));
 //find a specific object
 Person myLocatedObject = myList.Find(delegate(Person p) {return p.ID == 1; });
 }
 List 属于Generics ,同样在命名空间 Generics 下,这是 最快查找对象的方法。

原文地址:https://www.cnblogs.com/furenjian/p/2975309.html