Skip to content

Yield Return

Overview

In C#, yield return is a language feature that allows a method to return a sequence of values one at a time, rather than returning all the values at once. The method that uses yield return is called an iterator method, and it's defined using the yield keyword along with the return keyword.

When the iterator method is called, it returns an instance of an iterator object. The caller can then use this object to iterate through the sequence of values returned by the method. The sequence is produced on-the-fly as the caller iterates through the values, rather than being generated all at once and stored in memory.

Here's an example that demonstrates how yield return can be used to generate a sequence of even numbers:

public static IEnumerable<int> GetEvenNumbers(int start, int end)
{
    for (int i = start; i <= end; i++)
    {
        if (i % 2 == 0)
        {
            yield return i;
        }
    }
}
In this example, the GetEvenNumbers method returns an IEnumerable<int> that produces even numbers in the range between start and end. The yield return statement is used to produce each even number one at a time as the caller iterates through the sequence.

There are many use cases for yield return, including:

  • Generating large sequences of data that would be impractical to generate and store all at once.
  • Lazily loading data from a database or other data source as it is needed.
  • Implementing custom iterators for custom data structures or algorithms.
  • Implementing state machines that produce a sequence of output values based on a sequence of input values.

  • Detail tuturial : https://youtu.be/AAz8q6dOCYk