find and solve || findandsolve.com
Please wait.....

What is the yield keyword used for in C#

When we use the yield contextual keyword in a statement in c# programming language, you indicate that the operator, method or get accessor in which it appears is an iterator.

The function returns an object that implements the IEnumerable<object> interface. If we calling function starts reaching over this object, the function is called again until it "yields" the keyword. This is syntactic sugar introduced in C# 2.0. In earlier versions, we had to create our own IEnumerable and IEnumerator objects to do stuff like this.


The following is the syntax of defining the yield keyword in the c# programming language.

yield return <expression>;

yield break;

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


C# Yield Keyword Requirements


The declaration of yield must meet the following requirements.

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


In c# programming language, you can consume the iterator method that contains a yield return statement either by using a for-each loop or LINQ query.


C# Yield Keyword Example


In the following 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 FindAndSolve
{
    public class Program     {
       public static void Main(string[] args)
       {
          foreach (int j in MT(2, 8))
          {
             Console.Write("{0} ", j);
          }
          Console.ReadLine();
       }
       public static IEnumerable<int> MT(int n, int r)
       {
          int result = 1;
          for (int i = 1; i < r; i++)
          {
             result = result * n;
             yield return result;
          }
       }
    }
}


Output


2 4 8 16 32 64 128 


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


C# Yield Keyword Overview


  • yield is a keyword in c# which is useful to indicate that the method, operator, or property in which it appears is an iterator.
  • In the C# programming language 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.
  • In the C# 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 the try-finally statement.
  • The yield break statement can be used in try or catch block but not in finally block.
  • You can consume the iterator method that contains a yield return statement either by using a foreach loop or LINQ query.