C# & CLR Internals (50 Qs)•Part 3: CLR Internals, Native AOT & Production Diagnostics (25 - 35)
LINQ Internals and Deferred Execution Traps.
senior LevelcsharpCLR Internals
In Simple Words:
LINQ uses Deferred (Lazy) Execution: query execution does not occur when the LINQ query is defined, but when enumerated (foreach, .ToList(), .Count()):
- Multiple Enumeration Bug: Calling
.Count()and then.ToList()on anIEnumerablethat executes an HTTP request or DB query executes the expensive operation twice! Materialize with.ToList()or.ToArray()before multiple uses.