Yield return, what does it mean?



Yield return, it’s a very strong keyword, all the LINQ to object use it when we use foreach or yield return.

It is used to iterate through a collection returned by a method. you can create methods that return a collection and don’t have to go through declare and maintain it’s state the compiler will do it for you, what does it means?

To understand the different lets see code that doesn’t use yield and code does

Without yield

class Program
 {
   static void Main(string[] args)
   {
     foreach (int i in GetInt())
     {
       Console.WriteLine(i.ToString());
       Console.Read();
     }
   }

   public static IEnumerable<int> GetInt()
   {
      List<int> nums = new List<int>();
      for (int i = 0; i < 5; i++)
      {
         nums.Add(i);
      }
      return nums;
   }
 }

With yield


class Program
 {

   static void Main(string[] args)
   {
      foreach (int i in GetInt())
      {
        Console.WriteLine(i.ToString());
        Console.Read();
      }
   }

 public static IEnumerable<int> GetInt()
 {
   for (int i = 0; i < 5; i++)
   {
     yield return i;
   }
   yield return 20;
   yield return 30;
 }

You can see here the different that you don’t need to declare a variable to hold the return collection from your method you just need to yield return the variable and if you make a break point and execute each method you will notice how this method has different execution path, because without yield the foreach of the GetInt has to be finished first and then populate the collection variable but in other case it will return after each iteration, so it will return what you real need as you need it, so it will continue in the second method where it stop.

In other word it will get iterate to bring the first number and then the caller will return to the first method “Main” and then when the method inside the Main call the second method it will go to iterate and bring the second number and return and so on.

About these ads

3 thoughts on “Yield return, what does it mean?

  1. Very nice explanation bro,
    But let’s discuss it more…..
    Thinking that yield is useless or its just to ease your job instead of writing more code is totally wrong.
    “Yield return, it’s a very strong keyword, in in spite of I will not use it because, with LINQ to object you don’t need for-each or yield return”.
    This is the exact thing I don’t agree with you on. every time you use where or select or mostly any LINQ extension method you use for-each

    check my post here, i couldn’t post much in the comment:

    http://mohammedumar.wordpress.com/2011/11/15/why-the-yield-return-is-important/

    • Thanks for you comment Mohamed, but you missed my intent here!
      All what I want to say as long as I just use the built in extension LINQ methods, no need to write for each by hand, because I just use the extension method like where or select, but I didn’t say by using select or where it will not use for each, I mean I will not write for each as before, I hope you get my point :-)

      This of course unless I create my own extension or I just need to iterate over any collection to return some of them in this case sure if you are not use yield it’s a crime as you said :-)

  2. Exactly Radwan, any time you want to return collection its better to return it lazy, better for performance and for usability,

    Think about it, why return the whole collection at once while you can lazy return it ? and support Linq2Object laziness ?

    This apply to repositories or any other logic software architecture not only when making extensions. as there is many situations when you must for-each or loops no matter what, where and select cant be out of the box solution to all situations.

Leave a Reply

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 )

Connecting to %s