Hello everyone Please let me know what is yield and how to use this keyword. Please fully explain with an example.
Loading
Hello everyone Please let me know what is yield and how to use this keyword. Please fully explain with an example.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Uttam KumarPosted Apr 25, 2022, 9:04 AM
To understand Yield you must understand the difference between return and yield return. Lets see the with example
static int SimpleReturn()
{
return 1;
return 2;
return 3;
}
static void Main(string[] args)
{
Console.WriteLine(SimpleReturn());
Console.WriteLine(SimpleReturn());
Console.WriteLine(SimpleReturn());
Console.WriteLine(SimpleReturn());
}
Output:
1
1
1
1
In above example int SimpleReturn function three returns are mentioned but no matter how many times this function is called from anywhere, here from main the function will always return first value i.e 1.
Yield statement in C#
Example to demonstrate yield statement:
static IEnumerable YieldReturn()
{
yield return 1;
yield return 2;
yield return 3;
}
static void Main(string[] args)
{
foreach (int i in YieldReturn())
{
Console.WriteLine(i);
}
}
Output:
1
2
3
The only difference between yield and return is whenever yield statement is encountered in a function, the execution of function is suspended and a value is send back to the caller but because of yield whenever the function is called again, the execution of function begin where it left off previously. When resumed, the function continues execution immediately after the last yield run. Thus yield allows a function to produce a series of values over time. The only requirement for yield return statement is that the function containing yield should return an IEnumerable and no matter from where that function is called it should be called from an iteration block i.e foreach statement.
Muhammad Imran AnsariPosted Apr 25, 2022, 10:59 AM
The yield keyword, first introduced in C# 2.0, T returns an object that implements the IEnumerable interface.
The yield keyword is used to build generators of element sequences. A standard case, create a method that generates the sequence you need. The only limitation here is that the method must return one of the following types: IEnumerable, IEnumerable, IEnumerator or IEnumerator.
yield can be used in methods, properties and operators. See the below simple code:
static IEnumerable GenerateFibonacciNumbers(int n)
{
for (int i = 0, j = 0, k = 1; i < n; i++)
{
yield return j;
int temp = j + k;
j = k;
k = temp;
}
}
static void Main(string[] args)
{
foreach (int x in GenerateFibonacciNumbers(10))
{
Console.WriteLine(x);
}
}
yield return j; returns Fibonacci numbers one by one without exiting the “for” loop. The state information is preserved, there is no need of creating an intermediate list or array to hold the fibonacci numbers that need to be generated and returned to the caller.
In other words, yield keyword react as a state machine to maintain state information.
The MSDN states: "When a yield return statement is reached in the iterator method, expression is returned, and the current location in code is retained. Execution is restarted from that location the next time that the iterator function is called."