Last active
August 29, 2015 13:56
-
-
Save khellang/9234818 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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