53
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
5
public class Program
6
{
7
public static void Main()
8
{
9
//obtain a list of pets
10
IEnumerable<Pet> pets = CreateListOfPets();
11
12
//pass the pets list into our GetDogs method and yield return the first 2 dogs we find.
13
foreach (var dog in GetDogsUsingYield(pets).Take(2))
14
{
15
Console.WriteLine("- retrieved: " + dog.Name + " the " + dog.Animal);
16
}
17
}
18
19
20
public static IEnumerable<Pet> GetDogsUsingYield(IEnumerable<Pet> pets){
21
foreach (var pet in pets)
22
{
23
Console.WriteLine("Processing with yield: " + pet.Animal);
24
if(pet.Animal=="Dog")
Cached Result