Search This Blog

Monday, December 23, 2013

Predicate Delegate C# Example

C# > System > Predicate<T> Delegate

This delegate is used to search for elements in the collection.
Predicate<T> delegate is represented by a lambda expression.

Example


public class Student
{
       public string Name { get; set; }
       public int Year { get; set; }
       public Student(string name, int year)
       {
              this.Name = name;
              this.Year  = year;
       }
}


List<Student> groups = new List<Student>();
groups.AddRange(new Student[] { new Student("Stud1", 1973),
                                new Student("Stud2", 2000),
                                new Student("Stud3", 2010),
                                new Student("Stud4", 1999),
                                new Student("Stud5", 1995) });


foreach (var g in groups.FindAll(x => x.Year >= 2000))
              System.Windows.MessageBox.Show(g.Name);