Lazy Evaluation

Their piecemeal on-demand evaluation characteristic makes iterators an ideal construct to express queries. Because the underlying data source can potentially change after you have declared a query expression, delaying the execution is a much desirable feature:

image

The query expression used here is compiled into a call to the Where query operator we’ve seen before. Because it’s realized using iterators, it’s only when we enumerate over the query object that execution starts to take place. Because of this, a query expression is nothing more than a declaration of the intent of the query.

That said, some query operators trigger immediate ...

Get C# 4.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.