I have seen this syntax in MSDN: yield break
, but I don't know what it does. Does anyone know?

- 24,871
- 12
- 85
- 147

- 30,624
- 33
- 94
- 146
-
4Yield return eliminates the need for a backing list, that is you don't need to code something like `MyList.Add(...)` just do `yield return ...`. If you need to break out of the loop prematurely and return the virtual backing list you use `yield break;` – The Muffin Man Aug 26 '13 at 21:16
9 Answers
It specifies that an iterator has come to an end. You can think of yield break
as a return
statement which does not return a value.
For example, if you define a function as an iterator, the 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 its 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 the last statement is never executed because we left the function early.

- 7,287
- 7
- 50
- 85

- 15,630
- 2
- 34
- 35
-
12Could it be simply `break` instead of `yield break` in your example above? Compiler doesn't complain on that. – orad Jun 03 '14 at 18:26
-
99@orad a simple `break` in this case would stop the loop, but it wouldn't abort the method execution, thus the last line would be executed and the "Won't see me text" would actually be seen. – Damir Zekić Jun 03 '14 at 20:27
-
@DamirZekić, does the iterator complete all cycles in the case of break? – machinarium Jul 10 '15 at 07:29
-
1@machinarium I'm not sure what exactly you're asking, but after `yield break` no other code in the iterator is going to be executed, so there would be no further cycles. – Damir Zekić Jul 12 '15 at 11:25
-
so it's when we don't want to conditionally skip the the remained yield returns... – Hassan Faghihi Oct 17 '15 at 08:28
-
2@deadManN you would use `yield break` to unconditionally skip remaining `yield return`s. – Damir Zekić Oct 19 '15 at 03:59
-
5@Damir Zekić Could you also add to your answer why you should prefer yield break over return and what are the differences from the two? – Bruno Costa Nov 27 '15 at 20:10
-
11@DamirZekić returning null and yield break **is not** the same. If you return null then you can have a `NullReferenceException` getting the enumerator of the IEnumerable, while with yield break you don't (there is an instance with no elements). – Bruno Costa Feb 10 '16 at 13:24
-
6@BrunoCosta Trying to have regular returns in the same method gives a compiler error. Using `yield return x` alerts the compiler you want this method to be syntactic sugar for creating an `Enumerator` object. This Enumerator has method `MoveNext()` and property `Current`. MoveNext() executes the method until a `yield return` statement, and turns that value into `Current`. The next time MoveNext is called, execution continues from there. `yield break` sets Current to null, signaling the end of this enumerator, so that a `foreach (var x in myEnum())` will end. – Wolfzoon Oct 10 '16 at 17:23
-
2@Wolfzoon Instead of seeing `yield break` as setting `Current` to nul, I see it as `MoveNext` returning false. The former returns a null value, the latter doesn't. – Bruno Costa Oct 11 '16 at 08:23
-
**Note** that in order to use this example you should put it inside a function, e.g. `public IEnumerable
GetEnumerable() { ... }` and then use it like `foreach (var e in GetEnumerable()) { e.Dump(); }`. To try it out, see [DotNetFiddle](https://dotnetfiddle.net/i3xRPm). – Matt May 14 '18 at 08:24
Ends an iterator block (e.g. says there are no more elements in the IEnumerable).

- 117,631
- 17
- 236
- 300
-
3with [+1] -- although academically there are no iterators in .NET. only enumerators (one direction, forward.) – Shaun Wilson May 13 '16 at 19:57
-
@Shaun Wilson, but with `yield` keyword you can iterate collection in both directions, moreover you can take every next element of collection not in a row – monstr Mar 08 '17 at 21:07
-
@monstr you can iterate collection in any fashion and you don't need `yield` to do it. i'm not saying you're wrong, i see what you're suggesting; but, academically there are no iterators in .NET, only enumerators (one direction, forward) -- unlike stdc++ there is no "generic iterator framework" defined in the CTS/CLR. LINQ helps close the gap with extension methods that utilize `yield return` **and also callback methods**, but they are first-class extension methods, not first-class iterators. the resulting `IEnumerable` can't itself iterate in any direction other than forward wrt the caller. – Shaun Wilson Mar 10 '17 at 20:51
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;
}
}

- 12,050
- 15
- 55
- 67
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.
yield break is just a way of saying return for the last time and don't return any value
e.g
// returns 1,2,3,4,5
IEnumerable<int> CountToFive()
{
yield return 1;
yield return 2;
yield return 3;
yield return 4;
yield return 5;
yield break;
yield return 6;
yield return 7;
yield return 8;
yield return 9;
}

- 952
- 2
- 9
- 16
The yield break
statement causes the enumeration to stop. In effect, yield break
completes the enumeration without returning any additional items.
Consider that there are actually two ways that an iterator method could stop iterating. In one case, the logic of the method could naturally exit the method after returning all the items. Here is an example:
IEnumerable<uint> FindPrimes(uint startAt, uint maxCount)
{
for (var i = 0UL; i < maxCount; i++)
{
startAt = NextPrime(startAt);
yield return startAt;
}
Debug.WriteLine("All the primes were found.");
}
In the above example, the iterator method will naturally stop executing once maxCount
primes have been found.
The yield break
statement is another way for the iterator to cease enumerating. It is a way to break out of the enumeration early. Here is the same method as above. This time, the method has a limit on the amount of time that the method can execute.
IEnumerable<uint> FindPrimes(uint startAt, uint maxCount, int maxMinutes)
{
var sw = System.Diagnostics.Stopwatch.StartNew();
for (var i = 0UL; i < maxCount; i++)
{
startAt = NextPrime(startAt);
yield return startAt;
if (sw.Elapsed.TotalMinutes > maxMinutes)
yield break;
}
Debug.WriteLine("All the primes were found.");
}
Notice the call to yield break
. In effect, it is exiting the enumeration early.
Notice too that the yield break
works differently than just a plain break
. In the above example, yield break
exits the method without making the call to Debug.WriteLine(..)
.

- 15,565
- 8
- 50
- 71
Here http://www.alteridem.net/2007/08/22/the-yield-statement-in-c/ is very good example:
public static IEnumerable<int> 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.

- 1,920
- 3
- 36
- 57
Apart from the other good answers, note that yield break
does not work like a normal break
when in comes to nested loops. Where a break
would only stop the current loop but not any outer loop, yield break
will stop the whole enumeration:
IEnumerable<int> Iterate() {
for(int i=0; i<5; i++) {
yield return i;
for(int j=0; j<5; j++) {
if ((i*10 + j) > 30)
// This will stop the whole enumeration, even if there's
// an outer "for" loop
yield break;
yield return (i*10 + j);
}
}
}
Console.WriteLine(string.Join(", ", Iterate().Select(i => i.ToString())));
// 0, 0, 1, 2, 3, 4, 1, 10, 11, 12, 13, 14, 2, 20, 21, 22, 23, 24, 3, 30

- 6,018
- 6
- 60
- 86
The yield keyword is used together with the return keyword to provide a value to the enumerator object. yield return specifies the value, or values, returned. When the yield return statement is reached, the current location is stored. Execution is restarted from this location the next time the iterator is called.
To explain the meaning using an example:
public IEnumerable<int> SampleNumbers() { int counter = 0; yield return counter; counter = counter + 2; yield return counter; counter = counter + 3; yield return counter ; }
Values returned when this is iterated are: 0, 2, 5.
It’s important to note that counter variable in this example is a local variable. After the second iteration which returns the value of 2, third iteration starts from where it left before, while preserving the previous value of local variable named counter which was 2.

- 12,127
- 17
- 61
- 79

- 1,930
- 3
- 24
- 27
-
11
-
I don't think `yield return` actually supports returning multiple values. Maybe that isn't what you actually meant, but that's how I read it. – Sam Oct 08 '13 at 03:33
-
Sam -- the SampleNumbers method with multiple yield return statements does in fact work, the value of the iterator is returned immediately and execution is resumed when the next value is requested. I have seen people end a method like this with "yield break", but its unnecessary. Hitting the end of the method also ends the iterator – Loren Paulsen May 16 '16 at 21:54
-
the reason this is a poor example for `yield break` is that it does not contain an language-level enumerator such as a `foreach` -- when using an enumerator the `yield break` provides real value. this example looks like an unrolled loop. you will almost never see this code in the real world (we can all think of some edge cases, sure) also, there is no "iterator" here. the "iterator block" cannot extend beyond the method as a matter of language specification. what is actually being returned is an "enumerable", see also: http://stackoverflow.com/questions/742497/yield-statement-implementation – Shaun Wilson Mar 10 '17 at 21:01