Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
745 views
in Technique[技术] by (71.8m points)

linq to nhibernate - Difference between deferred execution and Lazy evaluation in C#

Could you please let me know what is the exact deference between deferred execution and Lazy evaluation in C#?These two are used synonymously.Could some one please explain the difference with an example??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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."

In practice, 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.

Please use the term deferred when discussing the subject as it pertains to C#. Lazy is a vaguer version.

Note: Lazy execution/evaluation is different from lazy loading or fetching in the context of a sequence. Lazy loading and eager loading are terms used to describe how elements of a sequence are loaded. When a sequence is loaded lazily, it means that whatever is generating the sequence does just enough work to load one element at a time. When a sequence is eagerly loaded, the entire sequence is loaded all at once and stored in a local buffer. Different usage requirements call for different loading patterns.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...