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

“What does ‘yield break;’ do in C#?”; StackOverflow.com

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.

[http://stackoverflow.com/questions/231893/what-does-yield-break-do-in-c]

mod date: 2009-08-18T06:22:42.000Z