Can someone please explain me what I am missing here. Based on my basic understanding linq result will be calculated when the result will be used and I can see that in following code.
static void Main(string[] args)
{
Action<IEnumerable<int>> print = (x) =>
{
foreach (int i in x)
{
Console.WriteLine(i);
}
};
int[] arr = { 1, 2, 3, 4, 5 };
int cutoff = 1;
IEnumerable<int> result = arr.Where(x => x < cutoff);
Console.WriteLine("First Print");
cutoff = 3;
print(result);
Console.WriteLine("Second Print");
cutoff = 4;
print(result);
Console.Read();
}
Output:
First Print 1 2 Second Print 1 2 3
Now I changed the
arr.Where(x => x < cutoff);
to
IEnumerable<int> result = arr.Take(cutoff);
and the output is as follow.
First Print 1 Second Print 1
Why with Take, it does not use the current value of the variable?
There’s a few different things getting confused here.
Late-binding: This is where the meaning of code is determined after it was compiled. For example,
x.DoStuff()is early-bound if the compiler checks that objects ofx‘s type have aDoStuff()method (considering extension methods and default arguments too) and then produces the call to it in the code it outputs, or fails with a compiler error otherwise. It is late-bound if the search for theDoStuff()method is done at run-time and throws a run-time exception if there was noDoStuff()method. There are pros and cons to each, and C# is normally early-bound but has support for late-binding (most simply throughdynamicbut the more convoluted approaches involving reflection also count).Delayed execution: Strictly speaking, all Linq methods immediately produce a result. However, that result is an object which stores a reference to an enumerable object (often the result of the previous Linq method) which it will process in an appropriate manner when it is itself enumerated. For example, we can write our own
Takemethod as:Now, when we use it:
Captured variables: Normally when we make use of a variable, we make use of how its current state:
It’s pretty intuitive that this prints
2andabcand not3andxyz. In anonymous functions and lambda expressions though, when we make use of a variable we are “capturing” it as a variable, and so we will end up using the value it has when the delegate is invoked:Creating the
λdoesn’t use the values ofiands, but creates a set of instructions as to what to do withiandswhenλis invoked. Only when that happens are the values ofiandsused.Putting it all together: In none of your cases do you have any late-binding. That is irrelevant to your question.
In both you have delayed execution. Both the call to
Takeand the call toWherereturn enumerable objects which will act uponarrwhen they are enumerated.In only one do you have a captured variable. The call to
Takepasses an integer directly toTakeandTakemakes use of that value. The call toWherepasses aFunc<int, bool>created from a lambda expression, and that lambda expression captures anintvariable.Whereknows nothing of this capture, but theFuncdoes.That’s the reason the two behave so differently in how they treat
cutoff.