In the tests below, I cannot get Console.WriteLine to really print when using yield return.
I’m experimenting with yield return and I understand I have something missing in my understanding of it, but cannot find out what it is. Why aren’t the strings printed inside PrintAllYield?
Code:
class Misc1 {
public IEnumerable<string> PrintAllYield(IEnumerable<string> list) {
foreach(string s in list) {
Console.WriteLine(s); // doesn't print
yield return s;
}
}
public void PrintAll(IEnumerable<string> list) {
foreach(string s in list) {
Console.WriteLine(s); // surely prints OK
}
}
}
Test:
[TestFixture]
class MiscTests {
[Test]
public void YieldTest() {
string[] list = new[] { "foo", "bar" };
Misc1 test = new Misc1();
Console.WriteLine("Start PrintAllYield");
test.PrintAllYield(list);
Console.WriteLine("End PrintAllYield");
Console.WriteLine();
Console.WriteLine("Start PrintAll");
test.PrintAll(list);
Console.WriteLine("End PrintAll");
}
}
Output:
Start PrintAllYield
End PrintAllYield
Start PrintAll
foo
bar
End PrintAll
1 passed, 0 failed, 0 skipped, took 0,39 seconds (NUnit 2.5.5).
You have to actually enumerate the returned
IEnumerableto see the output:When you use the
yield returnkeyword, the compiler will construct a state machine for you. Its code will only be run when you actually use it to iterate the returned enumerable.Is there a particular reason you’re trying to use
yield returnto print out a sequence ofstrings? That’s not really the purpose of the feature, which is to simplify the creation of sequence generators, rather than the enumeration of an already generated sequence.foreachis the preferred method for the latter.