// This seems reasonable...
public class Program
{
static void Main(string[] args)
{
Console.WriteLine("***** Fun with IEnumerable / IEnumerator *****\n");
Garage carLot = new Garage();
// Hand over each car in the collection?
foreach (Car c in carLot)
{
Console.WriteLine("{0} is going {1} MPH",
c.PetName, c.Speed);
}
Console.ReadLine();
}
}
Sadly, the compiler informs you that the Garage class does not implement a method named
GetEnumerator(). This method is formalized by the IEnumerable interface, which is found lurking
within the System.Collections namespace. Types that support this behavior advertise that they are
able to expose contained subitems to the caller (in this example, the foreach keyword itself ):
C H A P T E R 9
■
W O R K I N G W I T H I N T E R FA C E S
290
8849CH09.qxd 10/1/07 10:41 AM Page 290
Do'stlaringiz bilan baham: |