Skip to content

Instantly share code, notes, and snippets.

@khellang
Last active August 29, 2015 13:56
Show Gist options
  • Save khellang/9234818 to your computer and use it in GitHub Desktop.
Save khellang/9234818 to your computer and use it in GitHub Desktop.
public class AnimalFactory
{
public IEnumerable<IAnimal> GetAnimals()
{
yield return new Dog();
var ducks = GetDucks();
// return ducks; <-- **BOOM** Cannot return a value from an iterator.
foreach (var duck in ducks)
{
yield return duck; // <-- Works ;)
}
// yield return foreach ducks; <-- I wish this worked...
yield return new Cow();
}
private static IEnumerable<Duck> GetDucks()
{
return Enumerable.Range(0, 10).Select(x => new Duck());
}
}
public interface IAnimal { }
public class Duck : IAnimal { }
public class Cow : IAnimal { }
public class Dog : IAnimal { }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment