Yield Keyword in C#

Yield keyword
 
Yield is one of the most useful but under-used keywords in C#. The reason is that most of us don't even know about this keyword and the purpose it can serve for us. In this article, we will be discussing this keyword to understand the basic functionality that this keyword provides.
 
The functionality this keyword provides is that when iterating a list, we can read an element of the loop, return to the calling code and go back to the loop again at the same point, from where it left the loop and continue processing the records in the loop. So this will be the basic idea behind the example that we will be using. 
 
To demonstrate the use of the yield keyword, we will be creating a list with random numbers from 1-100. Our purpose will be to filter this list to get the numbers greater then 50 (without using any temporary list). So we first create a list and add some elements to it. Next we will create a method that will return an IEnumerator type. This will be the method in which the elements will be iterated and the result will be filtered to get the result list. We will pass the main list to this method and get the results returned from this method. So our code will be like the following:
Yield keyword 
 
So what is actually happening here is that the iteration starts over the list. It checks the first value, 31, that does not matches our selection criteria. So it continues up to fourth element in the same way, that is 53. Here, it encounters the yield statement. So what it does is that it stops the loop iteration and returns to the calling statement again, which is the GetData function. Then after adding it to the list called _data, it returns to the loop, but this time, it starts the operation from the next element in the list, in other words it starts processing the fifth element, 87. In this way it goes back to the calling code again and returns to process the next element at the sixth position, 89. So it continues to iterate until the loop is completed or a break statement is executed.
 
Run the application and see the results. We have the desired result without any need of temporary lists.
 
One very interesting point is related to the advantage that the yield keyword provides. It is related to how actually the state of iteration is maintained in the entire process. Before this keyword was introduced, the entire process of managing the state of the item being processed and moving to the next item in the process includes cumbersome code, with the use of IEnumerator interface, that was a tedious task to do. But with the introduction of the yield keyword, we need not to be worried about how the state if the iteration to be managed. We simply need to use the yield keyword and everything else works as needed. If you are interested in more about this process then you can try out these 2 very interesting articles that I came across C# 2.0 Iterators and Yield Keyword - Custom Collection Enumerators and Creating and Using Custom Collection Enumerators.
 
So this was all about the concept of yield in C#. I hope you enjoyed reading it.


Similar Articles