Home » Tutorials » C# » Use of Yield in C#

Use of Yield in C#

Generate CREATE Index script for all indexes in database in Sql Server
Difference between Throw and Throw ex in C#

Use of Yield in C#

Quote from MSDN

“The yield keyword signals to the compiler that the method in which it appears is an iterator block. The compiler generates a class to implement the behavior that is expressed in the iterator block. In the iterator block, the yield keyword is used together with the return keyword to provide a value to the enumerator object. This is the value that is returned, for example, in each loop of a foreach statement. The yield keyword is also used with break to signal the end of iteration.”

Basically in two different scenarios yield could be used:

  1. Conditional iteration through a collection.
  2. Stateful iteration.

1. Conditional iteration through a collection using Yield.

To understand why we could use Yield keyword, we will take a simple example of iteration to understand Yield. Let’s understand below example first and then we will use Yield keyword as an alternative of simple conditional iteration.

Here we are trying to get all the numbers which are greater than 3 in list of Integer. We could also achieve same thing by using Yield Keyword as demonstrate below code, as a Yield is containing state (will explain you regarding statefull iteration using Yield in second point) Yield will return first possible value to caller and caller execute its code and then again yield will return value and caller will execute its code and so on….


2. Stateful iteration.

As I have explained in first point Yield remember the state, we could use Yield keyword in stateful iteration as well. To demonstrate this functionality we will be using running total example to understand in a better way.


Running Total using Yield

Running Total using Yield

Performance measurement of Yield keyword against normal iteration

What do you thing what should be the measurement result?? Will really Yield keyword use make our code faster or we have to compromise our performance to achieve readability in our code!!!

Let’s rewrite the first point code and try to measure timings by using stopwatch.




By using this experiment we could find that normal iteration has taken 1.17 ns and with Yield keyword iteration took 1.74 ns. Which stats that Yield is slower in this scenario. So I urge you to take measurement before choosing Yield keyword as using yield CLR has to create internal class at execution time. Sometime Yield could also reduce timing of execution.

Points to be remembered:

  1. Do not put “yield” in an unsafe block.
  2. Do not use ref or out keywords with the parameters of the method, operator or getter / setter.
  3. “Yield return” can only be placed in the try block if it is followed by a finally block.
  4. “Yield break” can be put in the try block and catch, but not placed in the finally block.
  5. Do not use “yield” of the anonymous method.
  6. Main disadvantage of yield is creation of additional iterator class. When sequence is finite and caller doesn’t have complex logic, yield is slower and allocates more memory. Usage of yield makes sense in cases of data processing when each collection computation causes large memory block allocation. Lazy nature of yield can help to avoid computation of elements that will be filtered. In such cases, yield significantly reduces memory consumption and CPU load.

You can download whole code of Use of Yield in C# from here.


Hello friends, My self Dhruv Sheth, I have more than 6 years of Industrial experience as a Software Developer. In my career I got chance to work with asp.net, c#,Web API, JavaScript, JQuery, WPF, WCF , Windows Application, PostScript, vb.net, GhostScript etc. So I have decided to share my knowledge with everyone and finally I have come up with TechnoThirsty.

Leave a Reply

Your email address will not be published. Required fields are marked *


Time limit is exhausted. Please reload CAPTCHA.

The content is copyrighted to technothirsty.com and may not be reproduced on other websites without permission from the owner.You may contact us using the information below.