LINQ Interview Question #76
What does deferred execution mean in LINQ?
Deferred Execution, Immediate Execution & Query Behavior Mid-Level Intermediate
Quick Interview Answer
Deferred execution means that defining a LINQ query does not necessarily execute it immediately. Many sequence-returning operators, including Where and Select, store the query logic and perform the work when the result is enumerated.
Detailed Explanation
Deferred execution means that defining a LINQ query does not necessarily execute it immediately. Many sequence-returning operators, including Where and Select, store the query logic and perform the work when the result is enumerated.
This allows queries to be composed before execution. It also means the data observed by a query can change between query definition and enumeration.
Code Example
var query = numbers.Where(n => n > 10);
// No enumeration yet.
foreach (var number in query)
{
Console.WriteLine(number);
}