13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>C#</strong> 2: simple iterators with yield statements167■ When the Current property is used, it has to return the last value we yielded.■ It has to know when we’ve f<strong>in</strong>ished yield<strong>in</strong>g values so that MoveNext can returnfalse.The second po<strong>in</strong>t <strong>in</strong> this list is the tricky one, because it always needs to “restart” thecode from the po<strong>in</strong>t it had previously reached. Keep<strong>in</strong>g track of the local variables (asthey appear <strong>in</strong> the method) isn’t too hard—they’re just represented by <strong>in</strong>stance variables<strong>in</strong> the state mach<strong>in</strong>e. The restart<strong>in</strong>g aspect is trickier, but the good news is thatunless you’re writ<strong>in</strong>g a <strong>C#</strong> compiler yourself, you needn’t care about how it’s achieved:the result from a black box po<strong>in</strong>t of view is that it just works. You can write perfectlynormal code with<strong>in</strong> the iterator block and the compiler is responsible for mak<strong>in</strong>g surethat the flow of execution is exactly as it would be <strong>in</strong> any other method; the differenceis that a yield return statement appears to only “temporarily” exit the method—youcould th<strong>in</strong>k of it as be<strong>in</strong>g paused, effectively.Next we’ll exam<strong>in</strong>e the flow of execution <strong>in</strong> more detail, and <strong>in</strong> a more visual way.6.2.2 Visualiz<strong>in</strong>g an iterator’s workflowIt may help to th<strong>in</strong>k about how iterators execute <strong>in</strong> terms of a sequence diagram. 2Rather than draw<strong>in</strong>g the diagram out by hand, let’s write a program to pr<strong>in</strong>t it out(list<strong>in</strong>g 6.5). The iterator itself just provides a sequence of numbers (0, 1, 2, –1) andthen f<strong>in</strong>ishes. The <strong>in</strong>terest<strong>in</strong>g part isn’t the numbers provided so much as the flow ofthe code.List<strong>in</strong>g 6.5Show<strong>in</strong>g the sequence of calls between an iterator and its callerstatic readonly str<strong>in</strong>g Padd<strong>in</strong>g = new str<strong>in</strong>g(' ', 30);static IEnumerable GetEnumerable(){Console.WriteL<strong>in</strong>e ("{0}Start of GetEnumerator()", Padd<strong>in</strong>g);for (<strong>in</strong>t i=0; i < 3; i++){Console.WriteL<strong>in</strong>e ("{0}About to yield {1}", Padd<strong>in</strong>g, i);yield return i;Console.WriteL<strong>in</strong>e ("{0}After yield", Padd<strong>in</strong>g);}Console.WriteL<strong>in</strong>e ("{0}Yield<strong>in</strong>g f<strong>in</strong>al value", Padd<strong>in</strong>g);yield return -1;}Console.WriteL<strong>in</strong>e ("{0}End of GetEnumerator()", Padd<strong>in</strong>g);...IEnumerable iterable = GetEnumerable();IEnumerator iterator = iterable.GetEnumerator();2See http://en.wikipedia.org/wiki/Sequence_diagram if this is unfamiliar to you.Licensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!