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, Wash. He has worked on several Microsoft products including Azure and Bing. James can be reached at [email protected].

comments powered by Disqus

Featured

Subscribe on YouTube