CSharp · Console
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
//obtain a list
IEnumerable<Pet> pets = CreateListOfPets();
foreach (var dog in GetDogs(pets).Take(2))
{
Console.WriteLine("retrieved: " + dog.Name + " the " + dog.Animal);
}
}
public static IEnumerable<Pet> GetDogs(IEnumerable<Pet> pets){
List<Pet> dogs = new List<Pet>();
foreach (var pet in pets)
{
Console.WriteLine("Processing: " + pet.Animal);
if(pet.Animal=="Dog")
{
dogs.Add(pet);
}
}
return dogs;
}
public static IEnumerable<Pet> CreateListOfPets(){
return new List<Pet>{
new Pet{Animal="Dog",Colour="Black", Name="Rocky"},
new Pet{Animal="Cat",Colour="Black", Name="Betty"},
new Pet{Animal="Dog",Colour="White", Name="Fluffy"},
new Pet{Animal="Elephant",Colour="Grey", Name="Nellie"},
new Pet{Animal="Dog",Colour="Brown", Name="Lassie"},
new Pet{Animal="Cat",Colour="Brown", Name="Mr Jinx"},
new Pet{Animal="Mouse",Colour="Brown", Name="Micky"},
new Pet{Animal="Cat",Colour="Black", Name="Tom"},
new Pet{Animal="Leonardo",Colour="Green", Name="Turtle"},
new Pet{Animal="Lizard",Colour="Brown", Name="Yoda"}
};
}
}
public class Pet{
public string Animal {get;set;}
public string Colour {get;set;}
public string Name {get;set;}
}