Absolutely! Let’s focus specifically on yield return in C# and how it works.


πŸ“Œ 1. What is yield return?

  • yield return is used inside an iterator method to return elements one at a time.

  • The method does not return the entire collection at once.

  • The compiler generates the IEnumerable/IEnumerator machinery automatically.


πŸ“Œ 2. Basic Syntax

IEnumerable<int> GetNumbers()
{
    yield return 1;
    yield return 2;
    yield return 3;
}
  • Each yield return pauses the method and returns a value to the caller.

  • When the caller requests the next value (via MoveNext() in foreach), the method resumes where it left off.


πŸ“Œ 3. Usage in a foreach Loop

foreach (var number in GetNumbers())
{
    Console.WriteLine(number);
}

Output:

1
2
3
  • The method executes just enough to produce the next value each time.

  • No collection is stored in memory β€” only the current state is preserved.


πŸ“Œ 4. Example: Lazy Evaluation

IEnumerable<int> GenerateNumbers(int max)
{
    for (int i = 1; i <= max; i++)
    {
        Console.WriteLine($"Generating {i}");
        yield return i;
    }
}
 
foreach (var n in GenerateNumbers(3))
{
    Console.WriteLine($"Received {n}");
}

Output:

Generating 1
Received 1
Generating 2
Received 2
Generating 3
Received 3
  • yield return produces values one by one, on demand.

  • Memory efficient for large sequences or infinite streams.


πŸ“Œ 5. Benefits of yield return

  1. Memory-efficient β†’ no need to build a full collection.

  2. Lazy evaluation β†’ elements generated only when requested.

  3. Simplifies iterator code β†’ no need to implement IEnumerable manually.


πŸ“Œ 6. Comparison: yield return vs returning a List

// Using List
IEnumerable<int> GetNumbersList()
{
    List<int> numbers = new List<int> {1, 2, 3};
    return numbers;
}
 
// Using yield return
IEnumerable<int> GetNumbersYield()
{
    yield return 1;
    yield return 2;
    yield return 3;
}
FeatureListYield Return
MemoryAll elements in memoryOnly current element stored
EvaluationImmediateLazy, on-demand
ImplementationManual collectionSimple, compiler handles state

πŸ“Œ 7. Key Notes

  • yield return can only appear inside methods returning IEnumerable / IEnumerator

  • Can be used in loops, conditionals, or any sequence generation

  • yield return preserves method state automatically


βœ… Tip:

  • Use yield return for large datasets, streaming data, or infinite sequences

  • Works beautifully with LINQ and foreach loops