Back to slide -- Keyboard shortcut: 'u'  previous -- Keyboard shortcut: 'p'  next -- Keyboard shortcut: 'n'          collections/collection/3/farm.cs - Animalfarm - GetGroup with yield return.Lecture 12 - slide 35 : 36
Program 9

using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Collections;

public class AnimalFarm: Collection<Animal>{

  // Return an iterator of all animals in group g
  public IEnumerable<Animal> GetGroup(AnimalGroup g){
    foreach(Animal a in this)
      if (a.Group == g) yield return a;
  }

  // Additional methods

}