first_page the funky knowledge base
personal notes from way, _way_ back and maybe today

“yield (C# Reference)”; MSDN

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;

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.

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.

[http://msdn.microsoft.com/en-us/library/9k7k7cf0(VS.80).aspx]

mod date: 2009-08-18T05:03:18.000Z