C# in Depth

Cover of C# in Depth
Order now (3rd edition)

Deferred execution and iterator blocks

Chapter 11: Query expressions and LINQ to Objects: 11.1.2

Created: 3/5/2008
Last updated: 6/21/2008

One area which apparently causes lots of people pain is understanding that iterator blocks really do only start to get used when you iterate over the results. That was true when we looked at iterators in chapter 6, and it's equally true if you use a custom iterator as a data source (or custom operator) in LINQ.

Possibly the biggest source of confusion is around parameter validation - if the method does parameter validation, you might instinctively expect it to throw an exception as soon as you call the method, even if you understand that data is only going to be processed when you iterate over the result. This is certainly desirable, and I think it's a pity the language doesn't (currently) support this scenario. I've blogged about this in more detail.