Skip to content

Searching a Generic List of user-defined Objects using a Lambda Expression in C#

It’s really simple:

class Person()
{
  public string Name { get; set; }
  public int Age { get; set; }
  public Person(string name, int age)
  {
    Name = name;
    Age = age;
  }
}

class Test()
{
  public Test()
  {
    List<Person> lp = new List<Person>();
    lp.Add("Bob Barker",61);
    lp.Add("Allison Chains" , 33);
    lp.Add("Oscar de la Hoya", 40);
    Person p = lp.Find(p => p.Age > 40);
    MessageBox.Show(p.Name); // pops up "Bob Barker";
  }
}
Published inDevelopment

Be First to Comment

Leave a Reply