Hooked on LINQ

Hooked on LINQ - Developers' Wiki
for .NET Language Integrated Query

Companion book for this site
LINQ to Objects Using C# 4.0:
Using and Extending LINQ to Objects and Parallel LINQ (PLINQ)
Quick Search

Advanced Search »

Deferred Execution

Modified: 2012/03/20 10:11 by kristagiang - Categorized as: LINQ to Objects
Many of the Standard Query Operators return elements when iterated over using the foreach statement. These operators essentially do no work UNTIL the first element is requested, and then suspend until the next element is requested. Until the returned query is iterated over, no work is carried out internally for any of these statements. This deferred execution allows you to control when and if potentially lengthy work is carried out.

If you want a query to be realized immediately, call ToList, ToArray or any of the other operators that need to enumerate the entire sequence to return a result.

LINQ leverages a language feature added in C# 2.0 that made it simpler when writing methods that returned one element at a time when iterated over. The yield return statement essentially suspends a loop by returning a value, picking up exactly where it left off next time a MoveNext is called.

The MSDN library describes operators that use Deferred Execution as:

This method is implemented using deferred execution. The immediate return value is an object that stores all the information required to perform the action. The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or For Each in Visual Basic.

vietnam visa

If you would like to comment on this page, click on the Discuss button located on the top-right of each page. Feel free to edit any mistakes or omissions you find. If you have an objection or find in-appropriate content then contact the administrator. This website is not affiliated with Microsoft®, all content and opinions are those of the specific author and some advice, solutions and article may contain unintentional errors - please use care. Other websites by this author: Focused Objective, Geek Speak Decoded.