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()
inforeach
), 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
-
Memory-efficient β no need to build a full collection.
-
Lazy evaluation β elements generated only when requested.
-
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;
}
Feature | List | Yield Return |
---|---|---|
Memory | All elements in memory | Only current element stored |
Evaluation | Immediate | Lazy, on-demand |
Implementation | Manual collection | Simple, compiler handles state |
π 7. Key Notes
-
yield return
can only appear inside methods returningIEnumerable
/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