15.02.2015 Views

C# 4 and .NET 4

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

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

142 ❘ ChaPTer 6 ArrAys And tuples<br />

yield statement<br />

Since the fi rst release of <strong>C#</strong>, it has been easy to iterate through collections by using the foreach statement.<br />

With <strong>C#</strong> 1.0, it was still a lot of work to create an enumerator. <strong>C#</strong> 2.0 added the yield statement for<br />

creating enumerators easily. The yield return statement returns one element of a collection <strong>and</strong> moves<br />

the position to the next element, <strong>and</strong> yield break stops the iteration.<br />

The next example shows the implementation of a simple collection using the yield return statement.<br />

The class HelloCollection contains the method GetEnumerator() . The implementation of the<br />

GetEnumerator() method contains two yield return statements where the strings Hello <strong>and</strong> World<br />

are returned:<br />

using System;<br />

using System.Collections;<br />

namespace Wrox.ProCSharp.Arrays<br />

{<br />

public class HelloCollection<br />

{<br />

public IEnumerator < string > GetEnumerator()<br />

{<br />

yield return "Hello";<br />

yield return "World";<br />

}<br />

}<br />

code snippet YieldDemo/Program.cs<br />

A method or property that contains yield statements is also known as an iterator<br />

block. An iterator block must be declared to return an IEnumerator or<br />

IEnumerable interface, or the generic versions of these interfaces. This block may<br />

contain multiple y i e l d r e t u r n or yield break statements; a return statement is<br />

not allowed.<br />

Now it is possible to iterate through the collection using a foreach statement:<br />

}<br />

public void HelloWorld()<br />

{<br />

var helloCollection = new HelloCollection();<br />

foreach (var s in helloCollection)<br />

{<br />

Console.WriteLine(s);<br />

}<br />

}<br />

With an iterator block the compiler generates a yield type, including a state machine, as shown with<br />

the following code segment. The yield type implements the properties <strong>and</strong> methods of the interfaces<br />

IEnumerator <strong>and</strong> IDisposable . In the sample, you can see the yield type as the inner class Enumerator .<br />

The GetEnumerator() method of the outer class instantiates <strong>and</strong> returns a new yield type. Within the yield<br />

type, the variable state defines the current position of the iteration <strong>and</strong> is changed every time the method<br />

MoveNext() is invoked. MoveNext() encapsulates the code of the iterator block <strong>and</strong> sets the value of the<br />

current variable so that the Current property returns an object depending on the position.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!