Yield keyword in C#

Yield is one of the most useful, but under used keyword in c#. The reason is that most of us don’t even know about this keyword & the purpose it can serve for us (At least i did not knew anything about it except that there is a keyword of this name) . In this article, we will be discussing this keyword to understand basic functionality which this keyword provides.

The functionality, this keyword provides is that while 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 next records in 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 aim will be to filter this list to get the numbers greater then 50 (without using any temporary list). So first, we create a list and add some elements to it. Next we will create a method which will return an IEnumerator type. This will be the method in which the elements will be iterated and 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 :


So what actually happening here is that the iteration starts over the list.It checks the first value which is  31 and does not matches our selection criteria. So it continues up-to 4th element in the same way, which 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 comes back to the loop, but this time, it starts the operation from the next element in the list i.e. it starts processing the 5th element, which is 87. In this way it goes back to calling code again and comes back to process the next element at 6th position, which is 89. So, it continues to iterate until the loop is completed or any 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 which yield keyword provides. It is related to how actually the state of iteration is maintained in the whole process. Before this keyword was introduced, the whole process of managing the state of the item being processed and moving to next item in the process included cumbersome code, with the use of IEnumerator interface, which 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 will be managed. We simply need to use the yield keyword and rest of the things work as we need. If you are interested in more about this process, 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#. Hope you enjoyed reading it.


About Jasminder

.Net developer and blogger by profession, keen to learn new technologies, love nature, music and cricket.
This entry was posted in C# and tagged . Bookmark the permalink.

I have a suggestion..

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s