List.Find() use of delegates
Delegates make it easier to find stuff in generic lists.
Let’s consider the following simple class:
-
Public Class Person{
-
private string m_name;
-
private int m_age;
-
-
// .Net3.0 syntax only.
-
public string Name{get;set}
-
public int Age{get;set}
-
-
public Person(string name, int age){
-
m_name = name;
-
m_age = age;
-
}
Instanciating :
-
List<Person> lstPpl = new List<Person>();
-
lstPpl.Add(new Person("Christian", 1));
-
lstPpl.Add(new Person("Noah", 0.3));
We can easily find Noah:
-
Person myson = lstPpl.Find(delegate(Person p) { return "Noah" == p.Name; });