The Yield Statement in C#
Another often overlooked C# statement that was introduced in .NET 2.0 is yield. This keyword is used to return items from a loop within a method and retain the state of the method through multiple calls. That is a bit hard to wrap your head around, so as always, an example will help;
public static IEnumerable<int> Range( int min, int max )
{
for ( int i = min; i < max; i++ )
{
yield return i;
}
}
Each time Range is called, it will return one value in the given range. Of interest is that it maintains the state between calls. The best way to think of it is that for the first call to the method, execution starts at the first line and continues until it hits a yield statement at which time it returns the value. The subsequent runs through the method continue execution at the statement after the yield, continuing to yield values or exiting at the end of the method.
Using this, you can write the following code;
foreach ( int i in Range( 10, 20 ) )
{
Console.Write( i.ToString() + " " );
}
Which will return the following;
10 11 12 13 14 15 16 17 18 19
Why bother you say? Why not just iterate through the numbers yourself? The answer lies in the fact that each call maintains state, so you don't have to. The above example doesn't really show you the power, so let's try a more complex example, calculating prime numbers.
public static IEnumerable<int> Primes( int max )
{
yield return 2;
List<int> found = new List<int>();
found.Add( 3 );
int candidate = 3;
while ( candidate <= max )
{
bool isPrime = true;
foreach ( int prime in found )
{
if ( prime * prime > candidate )
{
break;
}
if ( candidate % prime == 0 )
{
isPrime = false;
break;
}
}
if ( isPrime )
{
found.Add( candidate );
yield return candidate;
}
candidate += 2;
}
}
Notice that there are multiple yields within this method and that the state is maintained through each call. You can now print out all of the prime numbers below 50 with;
foreach ( int prime in Primes( 50 ) )
{
Console.Write( prime.ToString() + " " );
}
There is also the yield break statement. If a yield break statement is hit within a method, execution of that method stops with no return. Using this, the first method could be rewritten like this;
public static IEnumerable<int> Range( int min, int max )
{
while ( true )
{
if ( min >= max )
{
yield break;
}
yield return min++;
}
}
It is not as useful in this case, but I'll leave it to you to find more interesting reasons to break out of a loop. Also, even though I used the generic IEnumerable