Deferred execution versus immediate execution--what's the difference?

  • Thread starter Thread starter raylopez99
  • Start date Start date
R

raylopez99

I'm reading a book that talks about how some operations, like LINQ
queries, are "deferred execution" while other queries are "immediate
execution".

What is the difference?

RL
 
I'm reading a book that talks about how some operations, like LINQ
queries, are "deferred execution" while other queries are "immediate
execution".

What is the difference?

RL

Consider this code:

static void Main(string[] args) {

//An array of integers
int[] numbers = { 1, 2, 3, 4};

//query the array for all even numbers
var query = from n in numbers
select Square(n);

//iterate through the results
foreach (int i in query) {
Console.WriteLine(i.ToString());
}


Console.ReadLine();
}

public static double Square(double n) {
Console.WriteLine("Computing Square(" + n + ")...");
return Math.Pow(n, 2);
}

When you run it, you see this output:

Computing Square(1)...
1
Computing Square(2)...
4
Computing Square(3)...
9
Computing Square(4)...
16

Notice that the line that says "Computing Square" for each number is
not executed until the foreach is executed. In other words the Square
method is not called until you iterate the query. But if you change
the foreach to look like this (note the addition of ToList():

//iterate through the results
foreach (int i in query.ToList()) {
Console.WriteLine(i.ToString());
}

The result is this:

Computing Square(1)...
Computing Square(2)...
Computing Square(3)...
Computing Square(4)...
1
4
9
16

All the squares are calculated before they are displayed. The first
example is deferred execution of the query. The second example
(ToList) is immediate execution.

Hope this helps a little.

Chris
 
raylopez99 said:
I'm reading a book that talks about how some operations, like LINQ
queries, are "deferred execution" while other queries are "immediate
execution".

What is the difference?

Deferred execution means that nothing really happens until you start
iterating through the data.

Immediate execution means that the data starts flowing *immediately*.

For instance, the "Where" method just sets up a data pipeline which
will filter data appropriate *when the result is enumerated*. "Count"
however needs to count the data immediately - it can't give you a
result without fetching the data.
 
All the squares are calculated before they are displayed.  The first
example is deferred execution of the query.  The second example
(ToList) is immediate execution.

Hope this helps a little.

Thanks, it did help.

It's odd looking but I will compile it and post later this month if I
find bugs (doing my real day job now, no time to code). Var,
IEnumerable used in this context and "select" are new to me, but I'll
figure it out. Looks like you might have a few lines out of sequence
but that's no big deal.

RL
 
Looks like you might have a few lines out of sequence but that's no big deal.

No, the code Chris provided works fine. If you think that it's out of
sequence because the query expression is "from ... select ..." instead
of the other way round (like SQL) then that's just the way LINQ is -
and for good reason. It's a much more logical ordering, showing the
data flowing through the query from beginning to end.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top