#28:Deferred Execution-Linq

Most of the Query Operators Execute only when they are enumerated(IEnumerator Type-Move Next),but not when constructed.

var item = new List<int>();
 item.Add(2);
IEnumerable<int> result = item.Select(x => x*5);
//Query is Constructed
item.Add(3);
//But still the item will be added and output would be 10,15 instead of 10.
 //This is called deferred execution.Query executes only on enumerating(for-each) but not on construction.
foreach (var VARIABLE in result)
{
 Console.WriteLine(VARIABLE);
}

Note: Not All Query Operators involves Deferred Execution.For instance First Operator should Execute the Query when constructed and return the First Element.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s