45 out of 103 rated this helpful - Rate this topic

yield (C# Reference)

Used in an iterator block to provide a value to the enumerator object or to signal the end of iteration. It takes one of the following forms:

yield return <expression>;
yield break;

expression is evaluated and returned as a value to the enumerator object; expression has to be implicitly convertible to the yield type of the iterator.

The yield statement can only appear inside an iterator block, which might be used as a body of a method, operator, or accessor. The body of such methods, operators, or accessors is controlled by the following restrictions:

  • Unsafe blocks are not allowed.

  • Parameters to the method, operator, or accessor cannot be ref or out.

A yield statement cannot appear in an anonymous method. For more information, see Anonymous Methods (C# Programming Guide).

When used with expression, a yield return statement cannot appear in a catch block or in a try block that has one or more catch clauses. For more information, see Exception Handling Statements (C# Reference).

In the following example, the yield statement is used inside an iterator block, which is the method Power(int number, int power). When the Power method is invoked, it returns an enumerable object that contains the powers of a number. Notice that the return type of the Power method is IEnumerable, an iterator interface type.

// yield-example.cs
using System;
using System.Collections;
public class List
{
    public static IEnumerable Power(int number, int exponent)
    {
        int counter = 0;
        int result = 1;
        while (counter++ < exponent)
        {
            result = result * number;
            yield return result;
        }
    }

    static void Main()
    {
        // Display powers of 2 up to the exponent 8:
        foreach (int i in Power(2, 8))
        {
            Console.Write("{0} ", i);
        }
    }
}

Output

 
2 4 8 16 32 64 128 256 

For more information, see the following sections in the C# Language Specification:

  • 19.3 Iterators

  • 22 Iterators

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Corrected Power() Example Method
I was not able to get the Power() example method to compile the way it's presented. Here is the corrected version that worked for me in Visual Studio 2008:

public static IEnumerable<int> Power( int number, int exponent )
{
int counter = 0;
int result = 1;
while ( counter++ < exponent )
{
result = result * number;
yield return result;
}
}

SJ at Microsoft edit: The example works fine for me in Visual Studio 2010. It sounds as if you were not using the original using directives (System. Collections vs. System.Collections.Generic). In any case, the example was replaced some time ago. Thanks.
Notes

ADDITIONAL NOTES:
Starting from .NET 2.0 you also use System.Collections.Generic.IEnumerable<T> as return type of iterator block.


A yield statement can not be used in body of finally block.


Using try/finally blocks with yield return statement inside them is unsafe if iteration performed using MoveNext/Current methods. Execution of statements inside finally block is not guaranteed. They will be executed only after all values before block will be iterated and next one requested OR IDisposable.Dispose method called on IEnumerator. Finally statements will not be executed during garbage collection as compiler generated iterator class have no finalizer. Using with foreach is safe as compiler will generate call to Dispose.


CSTeam edit: Your points are noted in later versions of the topic. Thanks for the contribution.

  • 6/24/2006
  • TAG
More Notes
Yield cannot be used inside an an anonymous method (compiler error)


CSTeam edit: This restriction is noted in the topic. Thanks.
Good introduction to yield break and yield return
Here is a blog that explains the usage of yield return and yield break in a simple example

http://www.iserialized.com/a-quick-start-guide-to-yield-return-and-yield-break



Examples of use of yield
Some examples about the use of 'yield' at: http://www.codeproject.com/KB/collections/Enumerators.aspx
Use yield to get Evens

How about this?


using System;
using System.Collections;
using System.Collections.Generic;
public static IEnumerable<int> Mod0(IEnumerable<int> NumList, int denominator)
{
foreach (int i in NumList)
{
if (i % denominator == 0)
{
yield return i;
}
}
}

static void Main()
{
int[] NumArray = { 8, 4, 7, 0, 1, 5, 3, 2, 6 };
foreach (int j in Mod0(NumArray , 2))
{
//ASSERT: j is EVEN
}
}