You can write perfectly
normal code within the iterator block and the compiler is responsible for making sure
that the flow of execution is exactly as it would be in any other method; the difference
is that a yield return statement appears to only ???temporarily??? exit the method??”you
could think of it as being paused, effectively.
Next we??™ll examine the flow of execution in more detail, and in a more visual way.
6.2.2 Visualizing an iterator??™s workflow
It may help to think about how iterators execute in terms of a sequence diagram.2
Rather than drawing the diagram out by hand, let??™s write a program to print it out
(listing 6.5). The iterator itself just provides a sequence of numbers (0, 1, 2, ??“1) and
then finishes. The interesting part isn??™t the numbers provided so much as the flow of
the code.
static readonly string Padding = new string(' ', 30);
static IEnumerable
GetEnumerable()
{
Console.WriteLine ("{0}Start of GetEnumerator()", Padding);
for (int i=0; i < 3; i++)
{
Console.WriteLine ("{0}About to yield {1}", Padding, i);
yield return i;
Console.WriteLine ("{0}After yield", Padding);
}
Console.WriteLine ("{0}Yielding final value", Padding);
yield return -1;
Console.WriteLine ("{0}End of GetEnumerator()", Padding);
}
...
IEnumerable iterable = GetEnumerable();
IEnumerator iterator = iterable.GetEnumerator();
2 See http://en.
Pages:
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338