This article shows how you can use the Yield keyword in C# to return one item at a time from a collection of items. The article also shows what are practical scenarios. You will learn when you should prefer to return items in a collection via the Yield keyword.

When to Use the Yield Keyword

The Yield keyword in C# is used to return items from a collection of type IEnumerable. The most useful functionality of the Yield keyword is manifested in its ability to return one item at a time. This makes the Yield keyword extremely handy when you have a collection of very large objects which take a lot of time to be returned.

With the Yield keyword, you can return one object, process it, and then return the next object. This way, your application is not stuck for a longer period and objects will be processed as they arrive rather than waiting for the whole collection to return. Now you know how to use the Yield keyword. Let’s see how to use it.

How to Use the Yield Keyword

The next section compares how to return a collection of items in a usual way and how to return items iteratively using the Yield keyword.

Let’s first see how to return all items in a normal collection at once without the use of the Yield keyword.

The following script defines a GetMultiples() method which accepts two parameter values: num and count. The method returns an integer array that contains the first N multiples of the value passed to the “num” parameter whereas N is specified by the count parameter.

The integer array returned by the GetMultiple() method is iterated using a for loop and all the items are displayed on the console.

class Program
    {
        static void Main(string[] args)
        {
            int[] nums = new int[5];
            nums = GetMultiples(5, 7);

            foreach(int item in nums)
            {
                Console.WriteLine(item);
            }

            Console.ReadKey();
        }

        public static  int[] GetMultiples(int num, int count)
        {
            int[] numlist = new int[count];
            for (int i = 0; i < count; i++)
            {
                numlist[i] = num * (i + 1);
            }

            return numlist;
        }
    }

If you put a breakpoint after you call the GetMultiples() method, you will see that the integer array returned by the GetMultiples() method contains all the items. For instance, in the following screenshot, you can see that the nums array contains the first 7 multiples of 5. This shows that all the items are returned at once.

How to Use Yield Keyword

Here is the output of the script above:

When to Use Yield Keyword

 

Let’s now see how the Yield keyword can be used to return one item at a time from an IEnumerable type collection.

Look at the following script. Here you again declare the GetMultiples() method. However, instead of returning an integer array, the method here returns the IEnumerable type collection.

The GetMultiples() method contains a for loop which is similar to what you saw in the previous script. However in this case, you don’t have to declare any integer array. And the values to be returned are generated on the fly when the “yield return” statement is executed.

 class Program
    {
        static void Main(string[] args)
        {
            IEnumerable nums = GetMultiples(5, 7);

            foreach(int item in nums)
            {
                Console.WriteLine(item);
            }

            Console.ReadKey();
        }

        public static  IEnumerable GetMultiples(int num, int count)
        {
            for (int i = 0; i < count; i++)
            {
               yield return num * (i + 1);
            }

        }
    }

Inside the Main method, if you put a breakpoint after the GetMultiples() method and check what is returned, you will see that nothing is returned as shown below.

Yield Keyword C Sharp

The items from the GetMultiples() method are only returned when you iterate through the method variable itself. With each interaction of the for loop inside the Main method, the GetMultiples() method is called and the next item in the IEnumerable collection is returned. Here is the output of the above script.

C Sharp Yield Keyword