static void Main() { // Create new local object theZoo, then add mammals and birds. Zoo theZoo = new Zoo(); theZoo.AddMammal("Whale"); theZoo.AddMammal("Rhinoceros"); theZoo.AddBird("Penguin"); theZoo.AddBird("Warbler"); // Iterate through each name in the theZoo. foreach (string name in theZoo) { Console.Write(name + " "); } Console.WriteLine(); // Iterate through bird names in theZoo. foreach (string name in theZoo.Birds) { Console.Write(name + " "); } Console.WriteLine(); // Iterate through mammal names in theZoo. foreach (string name in theZoo.Mammals) { Console.Write(name + " "); } Console.WriteLine(); Console.ReadKey(); }
//<Snippet24> static void Main() { Zoo theZoo = new Zoo(); theZoo.AddMammal("Whale"); theZoo.AddMammal("Rhinoceros"); theZoo.AddBird("Penguin"); theZoo.AddBird("Warbler"); foreach (string name in theZoo) { Console.Write(name + " "); } Console.WriteLine(); // Output: Whale Rhinoceros Penguin Warbler foreach (string name in theZoo.Birds) { Console.Write(name + " "); } Console.WriteLine(); // Output: Penguin Warbler foreach (string name in theZoo.Mammals) { Console.Write(name + " "); } Console.WriteLine(); // Output: Whale Rhinoceros Console.ReadKey(); }
private void IteratorsUsage() { Zoo zoo = new Zoo(); zoo.AddMammal("Whale"); zoo.AddMammal("Rhinoceros"); zoo.AddBird("Penguin"); zoo.AddBird("Warbler"); PrintAnimalName(zoo); PrintAnimalName(zoo.Birds); PrintAnimalName(zoo.Mammals); }