c sharp

C# Yield

A yield is a keyword used for certain parts of the program where this keyword can be used relevantly, not in the whole source code. This yield compiler tells the compiler that it is present and used in the iterator block. The syntax for the yield keyword is quite simple. We simply use this keyword before the ‘return’ and the ‘break’ statements.

Yield return ;

Yield break;

Working of Yield in C#

The method in which the yield statement is present is consumed through an iterator loop like foreach. Every iteration inside the loop calls the function. The function’s code is executed until the yield returns, or the break statement is executed. The current status of the iteration inside the loop is retained, and the iteration goes for the next cycle and starts from where it was left in the past iteration.

Example 1

The implementation of yield in C sharp contains the calculation of the Fibonacci series. This series is formed by adding the last two numbers. So we will explain the working yield in this case. As we know, yield is used in the break and return value, so we will use yield with the return keyword as we need to return each iteration to form a Fibonacci series.

We use a foreach loop to display all the series obtained from the built-in Fibonacci series function inside the main program. This loop contains a limit of 10 numbers. A Fibonacci series up to the 10th number will be generated. The function is declared as static.

# Public static IEnumerable <int> GetFibonacciSeries(10))

The function GetFibonacciSeries contains three numbers from where we need to start the series; usually, it starts with 0. Three variables are used as two variables have the last two numbers of series that are yet generated, and the third one will produce the results of the last two variables. “a” and “b” are initialized as 0, whereas “c” is declared as 1. The loop will iterate till the Fibonacci series number is less than the number provided.

Inside the For loop, we have used yield to return the value of the variable.

Yield return b;

This will return the next calculated value first and then calculate the proceeding Fibonacci value. And then, these values are again used in the loop until the 10th iteration.

$ MCS file.cs

$ mono file.exe

Example 2

This example deals with searching in the Fibonacci series. Inside the main program, the element to be found is declared to be an integer type variable. Because this variable will be used in the program instead of the value. The maximum limit for the Fibonacci series is also mentioned and declared to the variable. This will be used for the comparison method later on. This limit number is the value that is the end for now for the Fibonacci series. The number to be searched should lie in the series less than 100 value.

A foreach loop is used to iterate and generate the Fibonacci series till the 100 number is approached. This loop contains the function call for the Fibonacci series, and inside the parameter, there are two variables. One is the element to be found, and the other is the maximum size of the Fibonacci series, which provides a limit for the numbers to be generated.

FindFibonacciNumber (elementToFind, maxElements))

Inside this loop, if the number is generated and a match is found, it is displayed on the console. Then the function that was called is declared to have two integer type variables to accept the numbers sent through the function call.

<int> FindFibonacciNumber (int n, int max)

The return type for this function is an integer type. Inside the function, a For loop is used, which contains the initial values of variables a, and b is 0, and the value for the third variable is declared as 1. Furthermore, a Boolean variable checks if the condition remains true; it iterates. The condition is true when the Fibonacci series generates a number that lies within the given range; otherwise, the condition is False, and the compiler comes out of the loop.

The check statement is declared inside the loop. If the statement checks if the number present in a variable is greater than the maximum number, then display a message to the console that the specific number to be searched is out of the range and is not present. Here the yield statement is used with a break.

Yield break;

Inside the loop, an if statement will again check if the value in the variable “b” is equal to the number to be found, and then the value will be returned through yield.

Yield return b;

And the program is terminal by following the yield with the break statement. In this way, this program contains both yield types, working with return and a break.

Outside the ‘if body’, while remaining in the loop, a temporary variable will be used to store the value of a Fibonacci number. Save the code and then execute it. You will see that the entered number is found, as we have used 21.

Changing the number from 21 to 20 will show the message that the number is not found.

Example 3

This example deals with generating the days of the week in a c sharp program. This program will elaborate on the working of yield along with SET Accessor.

Days.DaysOfWeek()

Inside this function, the function call for the day name is declared. And the day number with the name is displayed through the console value inside the loop. A function show is declared to have an integer type return value and takes the variable to store the day number as a parameter. Again the ‘for loop’ will yield the value if the condition remains true.

Another class is created to declare a function, DaysOfWeek. It will use the GET feature to obtain the value by sending the day name and the day of the week. Each time each new line will be generated depending on the day number shown from the loop. Here the yield keyword is used with the return.

A separate class is created for the Day function and day name to be worked properly; this function will give and return values using Get and SET features.

All the days’ numbers, along with the day names, are generated on the execution.

Conclusion

A yield keyword generates value in two ways while using the C sharp programming language. One is to use a return to return the next value calculated by the expression used. The second one is with the break statement to terminate the program by breaking the iteration process. These are the basic features always used inside the loop, as the iteration is inside the loops. A basic example of the yield is generating the Fibonacci series and searching within the series. We have explained it in the example implemented in the Ubuntu operating system.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.