Blog of Rob Galanakis (@robgalanakis)

Lazy iteration, potential benefit

At this point, most people are familiar with the deferred evaluation model of LINQ, using ‘yield’ statements. Since this is new for some teams at work, there’s been a question of when is it appropriate. Deferred evaluation is of most benefit when there is some chance the entire Enumerable will not be evaluated. And it is of dubious use when the source of the enumeration may change. Those are the two extremes- where and when one uses ‘yield’ and deferred evaluation will of course vary according to the task. But regardless, anyone using IEnumerable in .NET 3.5+ needs to understand the deferred evaluation model of LINQ/yield or they will introduce any number of subtle bugs.

Sorry for the length between posts, there was a lot of work and career stuff going on that should be winding down now (and I’ve been doing a lot of travel lately so I have a few blog posts queued up).

Leave a Reply