I have seen this syntax in MSDN, but I don't know what it does. Does anyone know?
Ends an iterator block (e.g. says there are no more elements in the IEnumerable).
http://www.alteridem.net/2007/08/22/the-yield-statement-in-c/
Key quote:
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;
}
}
Read the whole thing.
The C# iterators are very complicated. See Raymond Chen's blog for details on how it works http://blogs.msdn.com/oldnewthing/archive/2008/08/12/8849519.aspx
Tells the iterator that it's reached the end.
As an example:
public interface INode
{
IEnumerable<Node> GetChildren();
}
public class NodeWithTenChildren : INode
{
private Node[] m_children = new Node[10];
public IEnumerable<Node> GetChildren()
{
for( int n = 0; n < 10; ++n )
{
yield return m_children[ n ];
}
}
}
public class NodeWithNoChildren : INode
{
public IEnumerable<Node> GetChildren()
{
yield break;
}
}
It specifies that an iterator has come to an end. You can think of yield break
as return
statement which does not return value.
For example, if you define a function as iterator, a body of the function may look like this:
for (int i = 0; i < 5; i++) {
yield return i;
}
Console.Out.WriteLine("You will see me");
Note that after the loop has completed all cycles, the last line gets executed and you will see the message in your console app.
Or like this with yield break
:
int i = 0;
while (true) {
if (i < 5) {
yield return i;
} else {
// note that i++ will not be executed after this
yield break;
}
i++;
}
Console.Out.WriteLine("Won't see me");
In this case last statement is never executed because we left function early.
The whole subject of iterator blocks is covered well in this free sample chapter from Jon Skeet's book C# in Depth.
yield
basically makes an IEnumerable<T>
method behave similarly to a cooperatively (as opposed to preemptively) scheduled thread.
yield return
is like a thread calling a "schedule" or "sleep" function to give up control of the CPU. Just like a thread, the IEnumerable<T>
method regains controls at the point immediately afterward, with all local variables having the same values as they had before control was given up.
yield break
is like a thread reaching the end of its function and terminating.
People talk about a "state machine", but a state machine is all a "thread" really is. A thread has some state (I.e. values of local variables), and each time it is scheduled it takes some action(s) in order to reach a new state. The key point about yield
is that, unlike the operating system threads we're used to, the code that uses it is frozen in time until the iteration is manually advanced or terminated.
Here http://www.alteridem.net/2007/08/22/the-yield-statement-in-c/ is very good example:
public static IEnumerable Range( int min, int max ) { while ( true ) { if ( min >= max ) { yield break; } yield return min++; } }
and explanation, that if a yield break statement is hit within a method, execution of that method stops with no return. There are some time situations, when you don't want to give any result, then you can use yield break.