Could you explian what is the exact deference between deferred execution and Lazy evaluation in C#?

Technology CommunityCategory: LINQCould you explian what is the exact deference between deferred execution and Lazy evaluation in C#?
VietMX Staff asked 3 years ago

In practice, they mean essentially the same thing. However, it’s preferable to use the term deferred.

  • Lazy means “don’t do the work until you absolutely have to.”
  • Deferred means “don’t compute the result until the caller actually uses it.”

When the caller decides to use the result of an evaluation (i.e. start iterating through an IEnumerable<T>), that is precisely the point at which the “work” needs to be done (such as issuing a query to the database).

The term deferred is more specific/descriptive as to what’s actually going on. When I say that I am lazy, it means that I avoid doing unnecessary work; it’s ambiguous as to what that really implies. However, when I say that execution/evaluation is deferred, it essentially means that I am not giving you the real result at all, but rather a ticket you can use to claim the result. I defer actually going out and getting that result until you claim it.