Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the use of yield break? [duplicate]

Tags:

iterator

yield

c#

People also ask

When would you use the yield break statement?

You can access the EvenNumbers static property to display the even numbers between 1 and 10 at the console window using the code snippet given below. You can use the "yield break" statement within an iterator when there are no more values to be returned. The "yield break" statement is used to terminate the enumeration.

Is yield break necessary?

No this is not necessary. It will work: public static IEnumerable<int> GetDiff(int start, int end) { while (start < end) { yield return start; start++; } // yield break; - It is not necessary.

Is yield break the same as return?

In a normal (non-iterating) method you would use the return keyword. But you can't use return in an iterator, you have to use yield break . In other words, yield break for an iterator is the same as return for a standard method. Whereas, the break statement just terminates the closest loop.

What is the use of the yield keyword?

The yield keyword is use to do custom stateful iteration over a collection. The yield keyword tells the compiler that the method in which it appears is an iterator block. yield return <expression>; yield break; The yield return statement returns one element at a time.


To give a code example, say you want to write an iterator that returns nothing if the source is null or empty.

public IEnumerable<T> EnumerateThroughNull<T>(IEnumerable<T> source)
{
    if (source == null)
        yield break;

    foreach (T item in source)
        yield return item;
}

Without the yield break it becomes impossible to return an empty set inside an iterator.


yield break specifies that the method should stop returning results. "return" by itself would not be good enough, or could even lead to a bug, because the return type of the method has to be IEnumerable.

I am not sure why the return keyword is also needed. My best guess would be that it helps make the intention of the statement a little clearer.

If you are interested, this article explains what yield is doing behind the scenes

Behind the scenes of the C# yield keyword


yield break and break do completely different things! Look

for(int i = 0; i < 10; ++i) {
   if(i > 5) { break; }
   yield return i;
}

for(int v = 2710; v < 2714; v+=2) {
   yield return v;
}

for(int s = 16; s < 147; ++s) {
   if(s == 27) { yield break; }
   else if(s > 17) { yield return s; }
}

Output would be an IEnumerable of these values: 0, 1, 2, 3, 4, 5, 2710, 2712, 18, 19, 20, 21, 22, 23, 24, 25, 26

Here's some method which does nothing useful, but it illustrates that you can combine yield break and break into the same method, and they do two different things! Break just breaks out of the loop, yield break ends the method completely.

I guess the reason return isn't used is because you're not returning an IEnumerable yourself in the method. Besides, don't you think yield break is clearer than return, for this kind of iterative situation? I do personally.


It is illegal to use a return statement in an iterator block. Moreover, the break statement could serve a dual purpose within an iterator block if it were to be allowed to affect the iteration: it could be used to break out of a loop or out of a switch, and it could be used to break out of the whole iteration.

yield return and yield break and are two keywords in and of themselves, and they both seem to be necessary.