The Yield return key phase in C# is quite old. It was introduced in C# 2.0. But at that time I was quite new to C# and indeed was not writing any articls. The C# yield key phase always fascinates me and I always wanted to write an article about this mighty key phase.
The Yield return key phase maintains the state machine for a specific collection. Maintains a state machine? But how? What the CLR does is that wherever it sees a yield return key phase being used, the CLR implements an Enumerator pattern for that piece of code. This type of implementation helps the developer from all the types of plumbing that we would have otherwise done in the absence of the keyword. Suppose the developer is filtering a collection, iterating though the collection and then extracting those objects in some new collection. This kind of plumbing is quite monotonous.
As said earlier in this article, yield is not a keyword, in other words it can still be used as a variable. However yield return and yield break are key phrases. Every time the compiler executes a yield return, the generated code will return that value followed by the phase. What happens is that the compiler generates an enumerator that adds the WhileMoveNext return-current plumbing without loosing the memory from the stack. In other words, as long as there are more and more items the stack memory for that collection is kept intact. When the last item is hit, the MoveNext returns false and the stack is unbound and finally the block is executed. The function that uses the Yield return phase returns an IEnumerable<T> type. In other words, there is no need to write the same logic for various types and that again prevents type safe-plumbing.
This article shows how to use the yield return phase to get the list of all the prime numbers between some range of integers while still keeping my application responsive. If I would not have used the yield return phase in that case I could have used a new collection in the method and waited for that method to complete its operation and return the entire collection of prime numbers in one go. Though whatever code I have written here is to display the capability of the yield return phase. I am sure this functionality can be done in a much better way.
In my demo project I have used a function as shown below that uses the yield keyword phase:
- static IEnumerable<int> GetPrimes(int from, int to)
- {
- for (int i = from; i <= to; i++)
- {
- if (!_worker.CancellationPending)
- {
- bool isPrime = true;
- int limit = (int)Math.Sqrt(i);
- for (int j = 2; j <= limit; j++)
- if (i % j == 0)
- {
- isPrime = false;
- break;
- }
- if (isPrime)
- {
- yield return i;
- }
- }
- }
- }
The following are some of the basic rules that we should keep in mind while using yield return:
- Yield return should only return the expression whose type should be the type of iterator.
- Yield return should be inside an iterator block and the iterator block can be in a method body, operator function or property.
- Yield statement cannot appear in anonymous methods.

Gowtham RajamanickamPosted Mar 13, 2015, 1:17 AM
good...
NitinPosted Mar 11, 2015, 5:37 AM
good one
Tom MohanPosted Mar 8, 2015, 6:42 AM
helpful