GetEnumerator();
iterator.MoveNext();
Console.WriteLine ("Received {0}", iterator.Current);
iterator.MoveNext();
Console.WriteLine ("Received {0}", iterator.Current);
This time the ???stopping??? line is never printed. It??™s relatively rare that you??™ll want to terminate
an iterator before it??™s finished, and it??™s relatively rare that you??™ll be iterating
manually instead of using foreach, but if you do, remember to wrap the iterator in a
using statement.
We??™ve now covered most of the behavior of iterator blocks, but before we end
this section it??™s worth considering a few oddities to do with the current Microsoft
implementation.
6.2.4 Quirks in the implementation
If you compile iterator blocks with the Microsoft C# 2 compiler and look at the resulting
IL in either ildasm or Reflector, you??™ll see the nested type that the compiler has
generated for us behind the scenes. In my case when compiling our (evolved) first
iterator block example, it was called IterationSample.
d__0 (where
the angle brackets aren??™t indicating a generic type parameter, by the way). I won??™t go
through exactly what??™s generated in detail here, but it??™s worth looking at it in Reflector
to get a feel for what??™s going on, preferably with the language specification next to
you: the specification defines different states the type can be in, and this description
makes the generated code easier to follow.
Pages:
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345