List.Find() use of delegates

C/C++ — Tags : — admin @ 22:02

Delegates make it easier to find stuff in generic lists.
Let’s consider the following simple class:

  1. Public Class Person{
  2.   private string m_name;
  3.   private int m_age;
  4.  
  5.   // .Net3.0 syntax only.
  6.   public string Name{get;set}
  7.   public int Age{get;set}
  8.  
  9.   public Person(string name, int age){
  10.     m_name = name;
  11.     m_age = age;
  12.   }

Instanciating :

  1. List<Person> lstPpl = new List<Person>();
  2. lstPpl.Add(new Person("Christian", 1));
  3. lstPpl.Add(new Person("Noah", 0.3));

We can easily find Noah:

  1. Person myson = lstPpl.Find(delegate(Person p) { return "Noah" == p.Name; });

0 Comments »

Pas encore de commentaire.

Flux RSS des commentaires de cet article. TrackBack URI

Laisser un commentaire

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
(c) 2012 Random namespace | powered by WordPress with Barecity