Demystifying the C# Yield-Return Mechanism: Listing 4.

Selecting Person objects through filters.

List<Person> list1 = new List<Person>();
foreach (Person p in pList) {
  if (p.Name.StartsWith("A") == true)
    list1.Add(p);
}

List<Person> list2 = new List<Person>();
foreach (Person p in list1) {
  if (p.Age < 20)
    list2.Add(p);
}

foreach (Person p in list2)
  Console.WriteLine(p.ToString());

About the Author

Dr. James McCaffrey works for Microsoft Research in Redmond, WA. James has worked on several key Microsoft products such as Internet Explorer and Bing. James can be reached at jamccaff@microsoft.com.