Exclusive Clothing Sale On Soft & Soothe

Get up to 63% off on our latest collection.

Shop Now

Yield Return Statement in C# | 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>;
Docker

 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;
Docker

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.
1static void Main(string[] args) {
2 foreach(int i in Add()) {
3 Console.Write("{0} ", i);
4 }
5 Console.ReadLine();
6}
7public static IEnumerable < int > Add() {
8 var val = new List < int > ();
9 for (int i = 1; i < 10; i++) {
10 val.Add(i + 1);
11 }
12 return val;
13}
C#

Output


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.

1static void Main(string[] args) {
2 foreach(int i in Add()) {
3 Console.Write("{0} ", i);
4 }
5 Console.ReadLine();
6}
7public static IEnumerable < int > Add() {
8 var val = 1;
9 for (int i = 0; i < 10; i++) {
10 yield
11 return val + i;
12 }
13}
C#

Output


Below is the logic difference between Return and Yield return 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 program code,

1static void Main(string[] args) {
2 foreach(int i in PrintNumberLessThanFive()) {
3 Console.Write("{0} ", i);
4 }
5Console.ReadLine();
6}
7public static IEnumerable < int > PrintNumberLessThanFive() {
8 for (int num = 0; num < 10; num++) {
9 if (num < 5) {
10 yield
11 return num;
12 } else {
13 yield
14 break;
15 }
16 }
17}
C#


 

Output



-----------------------------------------------------------------------------------------------------

Share this

Related Posts

Previous
Next Post »

1 comments:

Write comments
April 30, 2023 at 11:17 AM delete

Hi. Tnx for the article.
But it has wrong screen result as output for Return. It must be "2 3 4 ..." - you have "1 2 3 4...".
On your code screen in first case, iteration starts from "i=1", in other "i=0".

Reply
avatar

Exclusive Clothing Sale On Soft & Soothe

Get up to 63% off on our latest collection.

Shop Now