C# Yield Keyword

In c#, yield is a keyword, and it is useful to indicate that the method, operator, or property in which it appears is an iterator.

 

Following is the syntax of defining the yield keyword in c#.

 

yield return <expression>;
yield break;

In the above statements, the yield return statement will return one element at a time, and the return type of yield keyword must always be IEnumerable or IEnumerator object of values. The yield break statement is useful to end the iteration.

 

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

C# Yield Keyword Requirements

The declaration of yield must meet the following requirements.

 

  • Don’t use ref or out keyword with the parameters of method, operator, or property.
  • The return type of yield must be IEnumerable or IEnumerator object of values.
  • In c#, the yield return statement can use in the try block of try-finally statement.
  • The yield break statement can use in try or catch block but not in finally block.

In c#, we can consume the iterator method that contains a yield return statement either by using foreach loop or LINQ query.

 

In foreach loop, each iteration will call the iterator method, and the iterator method will return an expression when a yield return statement is reached, and the current location of code is retained to restart the code execution from that location the next time the iteration method is called.

C# Yield Keyword Example

Following is the example of defining the yield return statement in a method that’s inside of the foreach loop to return the required values.

 

using System;
using System.Collections.Generic;

namespace TutlaneExamples
{
    class Program
    {
       static void Main(string[] args)
       {
          foreach (int i in Multiply(2, 10))
          {
             Console.Write("{0} ", i);
          }
          Console.ReadLine();
       }
       public static IEnumerable<int> Multiply(int number, int range)
       {
          int result = 1;
          for (int i = 1; i < range; i++)
          {
             result = result * number;
             yield return result;
          }
       }
    }
}

If you observe the above example, we created Multiply() method with a yield return statement that’s inside of for loop, and each iteration of the foreach loop will call the iterator method (Multiply) until it reaches yield return statement and the return type of iterator method we used is IEnumerable of int.

 

Here, the iterator method (Multiply) will return the value after executing the yield return statement and the current location of code is stored to restart the code execution from that location the next time when the iteration method is called.

 

For the first time, the Multiply method will execute all the lines of code, next time, the execution of the iterator body continues from the location (within for loop) where it left off, as shown below.

 

C# Yield Keyword Execution Process Diagram

 

When we execute the above example, we will get the result as shown below.

 

2 4 8 16 32 64 128 256 512

This is how the yield keyword is useful to reduce the lines of code execution in c#.

C# Yield Keyword Overview

Following are the important points which we need to remember about the yield keyword in c#.

 

  • yield is a keyword, and it is useful to indicate that the methodoperator, or property in which it appears is an iterator.
  • The yield keyword statement can be declared like yield return <expression> and yield break.
  • The return type of yield must be IEnumerable or IEnumerator object of values.
  • The yield return or yield break statements are not allowed to use in anonymous methods and in the methods that contain unsafe blocks.
  • In c#, the yield return statement can use in the try block of try-finally statement.
  • The yield break statement can be used in try or catch block but not in finally block.
  • We can consume the iterator method that contains a yield return statement either by using a foreach loop or LINQ query.