Difference between deferred execution and Lazy evaluation in C#
Asked Answered
E

2

18

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??

Eleusis answered 27/3, 2010 at 20:8 Comment(5)
Check out Jon Skeets blog post on the subject: http://msmvps.com/blogs/jon_skeet/archive/2010/03/25/just-how-lazy-are-you.aspxMarguerita
Alright, you win. Slow/lazy me :)Marguerita
Duplicate, by an user named with the same name of https://mcmap.net/q/665263/-deferred-execution-and-eager-evaluationFlowerless
deferred and Lazy are same.Jackstraw
Updated link to Jon Skeet's blog post: Just how lazy are you?Laboured
S
32

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.

Sclaff answered 27/3, 2010 at 20:44 Comment(1)
If someone needs a code to see how those two are working, i prepare Console App that shows difference between them: github.com/jakubmaguza/Lazy-vs-Deferred-vs-Normal/blob/master/…Embryo
T
4

In process Deferred/lazy evaluation are same.

LINQ queries use lazy (or deferred) evaluation, not actually fetching any data until the result is used.

In c# Deferred/lazy evaluation does the same process, a query is not executed, until it is requested.

Ex:

DataContext context = new DataContext(); 
 var query = from customer in context.Customers   
            where customer.City == "Delhi"  
            select customer; // Query does not execute here, it wont fetch                           
                              to database to retrieve data   

foreach (var Customer in query) // Query executes here  
{  
   Console.WriteLine(Customer.Name);  
}

It executes only if immediate executions is used like projection/aggregate functions.etc. Ex:

 DataContext context = new DataContext(); 
        var query = (from customer in context.Customers 
         where customer.City == "Delhi" 
        select  customer).Count(); // Query execute here

.Count() which triggers and executes the query immediately.

Teece answered 4/7, 2018 at 13:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.