Yield Keyword

Posted: February 23, 2014 in Framework 4.0 New Features
Tags: , ,

what is the use of Yield keyword?

Yield keyword helps to do custom and stateful iteration over a collection

Custom Iteration
namespace ConsoleApplication1
{
    class Program
    {
        static List<int> myList = new List<int>();

        static void Main(string[] args)
        {
            FillValues();
            foreach (int i in myList)
            {
                Console.WriteLine(i);
            }
            Console.ReadLine();
        }

        static void FillValues()
        {
            myList.Add(1);
            myList.Add(2);
            myList.Add(3);
            myList.Add(4);
            myList.Add(5);

        }
    }
}

If we run the above program the output would be displayed as below

Untitled

2

Let us suppose that the user want to display only numbers which are greater than a specific number ( say 3), then many developers would end up writing a piece of code as shown below:

namespace ConsoleApplication1
{
    class Program
    {
        static List<int> myList = new List<int>();

        static void Main(string[] args)
        {
            FillValues();
            foreach (int i in Filter())
            {
                Console.WriteLine(i);
            }
            Console.ReadLine();
        }

        static void FillValues()
        {
            myList.Add(1);
            myList.Add(2);
            myList.Add(3);
            myList.Add(4);
            myList.Add(5);
        }

        static IEnumerable<int> Filter()
        {
            List<int> temp = new List<int>();

            foreach(int i in myList)
            {
                if(i >3)
                {
                    temp.Add(i);
                }
            }
            return temp;
        }
    }
}

3

Notice that in the above piece of code we are using a temporary list. It would be good if we can write the same piece of code without using the temporary list. That is where exactly yield will help us.

        static IEnumerable<int> Filter()
        {
            foreach(int i in myList)
            {
                if(i >3)
                {
                    yield return i;
                }
            }

        }

4

Stateful Iteration

Let us suppose that we would like to find the running total of the list defined in the above code

5

We use a yield return statement to return each element one at a time.

namespace ConsoleApplication1
{
    class Program
    {
        static List<int> myList = new List<int>();

        static void Main(string[] args)
        {
            FillValues();
            foreach (int i in RunningTotal())
            {
                Console.WriteLine(i);
            }
            Console.ReadLine();
        }

        static void FillValues()
        {
            myList.Add(1);
            myList.Add(2);
            myList.Add(3);
            myList.Add(4);
            myList.Add(5);
        }

 static IEnumerable<int> RunningTotal()
 {
    int runningTotal = 0;
    foreach(int i in myList)
    {
      runningTotal  += i;
      yield return runningTotal;
    }
  }
}
}
Advertisements

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 )

Google+ photo

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

Connecting to %s