Deferred Execution & LINQ Iterator State Machines
1Concept
LINQ operators (`Where`, `Select`) do not execute when declared. They construct an iterator pipeline that executes element-by-element only when enumerated (`foreach`, `.ToList()`).
2Architecture Diagram
var query = data.Where(x => x > 10).Select(x => x * 2); // 0 CPU Work Done!
│
foreach (var item in query)
│ (State machine pulls 1 item at a time with O(1) memory footprint)
▼
[ Pipeline Execution: Filter ──> Transform ──> Yield Item ]3Code Example
C# 13 & .NET 9
using System;
using System.Collections.Generic;
using System.Linq;
public class DeferredExecutionDemo
{
public static void Main()
{
var numbers = new List<int> { 1, 2, 3 };
// Query constructed (NOT executed yet!)
var query = numbers.Select(n =>
{
Console.WriteLine($"Evaluating item: {n}");
return n * 10;
});
Console.WriteLine("Query defined. Adding 4 to list...");
numbers.Add(4);
Console.WriteLine("Executing enumeration now:");
foreach (var val in query)
{
Console.WriteLine($"Yielded: {val}");
}
}
}4Expected Output
Query defined. Adding 4 to list... Executing enumeration now: Evaluating item: 1 Yielded: 10 Evaluating item: 2 Yielded: 20 Evaluating item: 3 Yielded: 30 Evaluating item: 4 Yielded: 40
5Key Takeaways
- ✓Deferred execution sees modifications made to collections prior to enumeration.
- ✓Beware of multiple enumeration bugs: call `.ToArray()` or `.ToList()` if querying repeatedly.
- ✓Memory footprint is O(1) streaming rather than buffering the entire dataset.