13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>C#</strong> 2: simple iterators with yield statements165at F simple and correct even if MoveNext is called aga<strong>in</strong> after it’s first reported thatthere’s no more data available. To reset the iterator, we set our logical position backto “before the first element” H.Most of the logic <strong>in</strong>volved is fairly straightforward, although there’s lots of roomfor off-by-one errors; <strong>in</strong>deed, my first implementation failed its unit tests for preciselythat reason. The good news is that it works, and that we only need to implementIEnumerable <strong>in</strong> IterationSample to complete the example:public IEnumerator GetEnumerator(){return new IterationSampleIterator(this);}I won’t reproduce the comb<strong>in</strong>ed code here, but it’s available on the book’s website,<strong>in</strong>clud<strong>in</strong>g list<strong>in</strong>g 6.1, which now has the expected output.It’s worth bear<strong>in</strong>g <strong>in</strong> m<strong>in</strong>d that this is a relatively simple example—there’s not a lotof state to keep track of, and no attempt to check whether the collection has changedbetween iterations. With this large burden <strong>in</strong>volved to implement a simple iterator, weshouldn’t be surprised at the rarity of implement<strong>in</strong>g the pattern <strong>in</strong> <strong>C#</strong> 1. Developershave generally been happy to use foreach on the collections provided by the framework,but they use more direct (and collection-specific) access when it comes to theirown collections.So, 40 l<strong>in</strong>es of code to implement the iterator <strong>in</strong> <strong>C#</strong> 1, not <strong>in</strong>clud<strong>in</strong>g comments.Let’s see if <strong>C#</strong> 2 can do any better.6.2 <strong>C#</strong> 2: simple iterators with yield statementsI’ve always been the k<strong>in</strong>d of person who likes to stay up until midnight on ChristmasEve <strong>in</strong> order to open a present as soon as Christmas Day arrives. In the same way, Ith<strong>in</strong>k I’d f<strong>in</strong>d it almost impossible to wait any significant amount of time before show<strong>in</strong>gyou how neat the solution is <strong>in</strong> <strong>C#</strong> 2.6.2.1 Introduc<strong>in</strong>g iterator blocks and yield returnThis chapter wouldn’t exist if <strong>C#</strong> 2 didn’t have a powerful feature that cut down theamount of code you had to write to implement iterators. In some other topics theamount of code has only been reduced slightly, or has just made someth<strong>in</strong>g more elegant.In this case, however, the amount of code required is reduced massively. List<strong>in</strong>g 6.4shows the complete implementation of the GetEnumerator method <strong>in</strong> <strong>C#</strong> 2.List<strong>in</strong>g 6.4Iterat<strong>in</strong>g through the sample collection with <strong>C#</strong> 2 and yield returnMuchsimpler, isn’tit?public IEnumerator GetEnumerator(){for (<strong>in</strong>t <strong>in</strong>dex=0; <strong>in</strong>dex < values.Length; <strong>in</strong>dex++){yield return values[(<strong>in</strong>dex+start<strong>in</strong>gPo<strong>in</strong>t)%values.Length];}}Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!