Friday, August 27, 2010

Yield Break and Return

There are two types of yield statements - yield return (for returning the next item) and yield break (to signify the end of the iterator).

using System;
using System.Collections;

class Test
{
static void Main(string[] args)
{
foreach (string x in Foo())
{
Console.WriteLine (x);
}
}

static IEnumerable Foo()
{
yield return "Hello";
yield return "there";
}
}

Result is

Hello
there

(http://www.yoda.arachsys.com/csharp/csharp2/iterators.html)

No comments: