luni, 25 august 2014

Predicate Delegate C# Example

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 { getset; }
       public int Year { getset; }
       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); 

Niciun comentariu:

Trimiteți un comentariu