Yield Keyword In C#

In this article, we will learn about the Yield keyword in C#.

The yield keyword informs the compiler that the method in which it is used, is an iterator block over collection. An iterator block returns the IEnumerable as the output And the yield keyword is used to return values for the IEnumerable.

Yield Keyword in C#

We cant use the Yield keyword alone in C#, we have to use it either with a return or break statement.

yield return - specifies the next item value of the iterator

yield return <expression>;

 The yield return statement returns one item value at a time. The return type of the yield keyword is either the IEnumerator or IEnumerable.

yield-break - specifies the end of the iteration

yield break;

Important about using yield are:

  • lazy evaluation
  • deferred execution

The yield return and yield break statements are not allowed to use in the anonymous methods and the methods which contain unsafe blocks.

Keypoint to remember while using Yield

  • The return type of the yield should be the IEnumerator or IEnumerable object of values.
  • Should not use ref or out keyword with the parameters of method, property, or operator,.
  • In c#, the yield return statement can be used in the try block of the try-finally statement.
  • The yield break statement can be used in the try or catch block but not in the final block.
static void Main(string[] args) {
    foreach(int i in Add()) {
        Console.Write("{0} ", i);
    }
    Console.ReadLine();
}
public static IEnumerable < int > Add() {
    var val = new List < int > ();
    for (int i = 1; i < 10; i++) {
        val.Add(i + 1);
    }
    return val;
}

Output

Yield keyword in C#

In the above example, we return the entire list at a time, but what if we want to return an item with iteration, in that case, we use Yield return.

static void Main(string[] args) {
    foreach(int i in Add()) {
        Console.Write("{0} ", i);
    }
    Console.ReadLine();
}
public static IEnumerable < int > Add() {
    var val = 1;
    for (int i = 0; i < 10; i++) {
        yield
        return val + i;
    }
}

Output

Yield keyword in C#

Below is the logic difference between Return and Yield return in C#

Yield keyword in C#

Yield Break in C#

We use the yield break statement to break the iteration or loop and exit from the iterator block on particular conditions. The yield break statement is just like the normal break statement in C#.

The break statement is used to exit the loop just before its normal termination.

On the other side, the yield break statement is used in the iterator block to stop the iteration and indicate that there are no more values existing in the sequence.

Below is the code,

static void Main(string[] args) {
    foreach(int i in PrintNumberLessThanFive()) {
        Console.Write("{0} ", i);
    }
    Console.ReadLine();
}
public static IEnumerable < int > PrintNumberLessThanFive() {
    for (int num = 0; num < 10; num++) {
        if (num < 5) {
            yield
            return num;
        } else {
            yield
            break;
        }
    }
}

Yield keyword in C#

Output

Yield keyword in C#


Similar Articles