LINQ Interview Question #96
What is short-circuiting in LINQ?
Deferred Execution, Immediate Execution & Query Behavior Mid-Level Intermediate
Quick Interview Answer
Some LINQ operators stop enumeration as soon as they have enough information to produce a result. Examples include Any, First, FirstOrDefault, and Take.
Detailed Explanation
Some LINQ operators stop enumeration as soon as they have enough information to produce a result. Examples include Any, First, FirstOrDefault, and Take.
Short-circuiting can improve efficiency because the complete source may not need to be processed. The actual behavior of a remote provider depends on how it translates the operation.
Code Example
bool hasNegative =
numbers.Any(n => n < 0);
var firstLarge =
numbers.FirstOrDefault(n => n > 100);