Deferred Execution Example (C#)

This topic shows how deferred execution and lazy evaluation affect the execution of your LINQ to XML queries.


The following example shows the order of execution when using an extension method that uses deferred execution. The example declares an array of three strings. It then iterates through the collection returned by ConvertCollectionToUpperCase.

public static class LocalExtensions  
    public static IEnumerable<string>  
      ConvertCollectionToUpperCase(this IEnumerable<string> source)  
        foreach (string str in source)  
            Console.WriteLine("ToUpper: source {0}", str);  
            yield return str.ToUpper();  
class Program  
    static void Main(string[] args)  
        string[] stringArray = { "abc", "def", "ghi" };  
        var q = from str in stringArray.ConvertCollectionToUpperCase()  
                select str;  
        foreach (string str in q)  
            Console.WriteLine("Main: str {0}", str);  

This example produces the following output:

ToUpper: source abc  
Main: str ABC  
ToUpper: source def  
Main: str DEF  
ToUpper: source ghi  
Main: str GHI  

Notice that when iterating through the collection returned by ConvertCollectionToUpperCase, each item is retrieved from the source string array and converted to uppercase before the next item is retrieved from the source string array.

You can see that the entire array of strings is not converted to uppercase before each item in the returned collection is processed in the foreach loop in Main.

The next topic in this tutorial illustrates chaining queries together:

See Also