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.

162 CHAPTER 6 Implement<strong>in</strong>g iterators the easy wayother mean<strong>in</strong>gs of the word enumeration. I’ve used iterator and iterable throughout thischapter.) If a type implements IEnumerable, that means it can be iterated over; call<strong>in</strong>gthe GetEnumerator method will return the IEnumerator implementation, which is theiterator itself.As a language, <strong>C#</strong> 1 has built-<strong>in</strong> support for consum<strong>in</strong>g iterators us<strong>in</strong>g the foreachstatement. This makes it <strong>in</strong>credibly easy to iterate over collections—easier than us<strong>in</strong>ga straight for loop—and is nicely expressive. The foreach statement compiles downto calls to the GetEnumerator and MoveNext methods and the Current property, withsupport for dispos<strong>in</strong>g the iterator afterwards if IDisposable has been implemented.It’s a small but useful piece of syntactic sugar.In <strong>C#</strong> 1, however, implement<strong>in</strong>g an iterator is a relatively difficult task. The syntacticsugar provided by <strong>C#</strong> 2 makes this much simpler, which can sometimes lead to the iteratorpattern be<strong>in</strong>g worth implement<strong>in</strong>g <strong>in</strong> cases where otherwise it would have causedmore work than it saved.In this chapter we’ll look at just what is required to implement an iterator and thesupport given by <strong>C#</strong> 2. As a complete example we’ll create a useful Range class that canbe used <strong>in</strong> numerous situations, and then we’ll explore an excit<strong>in</strong>g (if slightly off-thewall)use of the iteration syntax <strong>in</strong> a new concurrency library from Microsoft.As <strong>in</strong> other chapters, let’s start off by look<strong>in</strong>g at why this new feature was <strong>in</strong>troduced.We’ll implement an iterator the hard way.6.1 <strong>C#</strong> 1: the pa<strong>in</strong> of handwritten iteratorsWe’ve already seen one example of an iterator implementation <strong>in</strong> section 3.4.3 whenwe looked at what happens when you iterate over a generic collection. In some waysthat was harder than a real <strong>C#</strong> 1 iterator implementation would have been, because weimplemented the generic <strong>in</strong>terfaces as well—but <strong>in</strong> some ways it was easier because itwasn’t actually iterat<strong>in</strong>g over anyth<strong>in</strong>g useful.To put the <strong>C#</strong> 2 features <strong>in</strong>to context, we’ll first implement an iterator that is aboutas simple as it can be while still provid<strong>in</strong>g real, useful values. Suppose we had a new typeof collection—which can happen, even though .NET provides most of the collectionsyou’ll want to use <strong>in</strong> normal applications. We’ll implement IEnumerable so that usersof our new class can easily iterate over all the values <strong>in</strong> the collection. We’ll ignore theguts of the collection here and just concentrate on the iteration side. Our collection willstore its values <strong>in</strong> an array (object[]—no generics here!), and the collection will havethe <strong>in</strong>terest<strong>in</strong>g feature that you can set its logical “start<strong>in</strong>g po<strong>in</strong>t”—so if the array had fiveelements, you could set the start po<strong>in</strong>t to 2, and expect elements 2, 3, 4, 0, and then 1to be returned. (This constra<strong>in</strong>t prevents us from implement<strong>in</strong>g GetEnumerator bysimply call<strong>in</strong>g the same method on the array itself. That would defeat the purpose ofthe exercise.)To make the class easy to demonstrate, we’ll provide both the values and the start<strong>in</strong>gpo<strong>in</strong>t <strong>in</strong> the constructor. So, we should be able to write code such as list<strong>in</strong>g 6.1 <strong>in</strong>order to iterate over the collection.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!