Tuesday, April 8, 2008

Deferred query execution

The query operators is that they execute not when constructed but when ecumerated (in other words, when MoveNext is called on its enumerator)

LINQ sample:

var numbers = new List();
numbers.Add(1);
var query = from n in numbers select n * 10;
numbers.Add(10);
foreach (int i in query)
{
Console.WriteLine(i);
}
Console.ReadLine();


The number 10 is added into the query result, this is called lazy evalution or deferred.
This feature seperate the query construction from query execution

No comments: