Deferred Execution - VB
[Table of Contents] [Next Topic]
Deferred execution is a topic related to lazy evaluation, but it is a topic worth discussing in its own right.
This blog is inactive.
New blog: EricWhite.com/blog
Blog TOCIn the previous topic, we saw this code as an example of lazy evaluation (once again, the code is in C#):
using System;
using System.Collections.Generic;
using System.Linq;
public static class MyExtension
{
public static IEnumerable<string> ToUpper(this IEnumerable<string> source)
{
foreach (string s in source)
{
Console.WriteLine("Yield returning: {0}", s);
yield return s.ToUpper();
}
}
}
class Program
{
static void Main(string[] args)
{
string[] sa = new[] {
"aaa",
"Bbb",
"CCc"
};
Console.WriteLine("Before using ToUpper()");
var sb = sa.ToUpper();
Console.WriteLine("After using ToUpper()");
Console.WriteLine("Before iterating the collection in sb");
foreach (string s in sb)
Console.WriteLine("Within iteration, s: {0}", s);
}
}
It produces the following output:
Before using ToUpper()
After using ToUpper()
Before iterating the collection in sb
Yield returning: aaa
Within iteration, s: AAA
Yield returning: Bbb
Within iteration, s: BBB
Yield returning: CCc
Within iteration, s: CCC
In addition to lazy evaluation, the above code also demonstrates deferred execution. The program doesn't even start to iterate over the source array until it iterates over the result collection.
Contrast that program to this (moving back to VB):
Module Module1
<System.Runtime.CompilerServices.Extension()> _
Public Function ToUpper(ByVal source As IEnumerable(Of String)) _
As IEnumerable(Of String)
Dim result As List(Of String) = New List(Of String)
For Each s In source
Console.WriteLine("Adding {0} to list", s)
result.Add(s)
Next
Return result
End Function
Sub Main()
Dim sa() As String = { "aaa", "Bbb", "CCc" }
Console.WriteLine("Before using ToUpper()")
Dim sb = sa.ToUpper()
Console.WriteLine("After using ToUpper()")
Console.WriteLine("Before iterating the collection in sb")
For Each s In sb
Console.WriteLine("Within iteration, s: {0}", s)
Next
End Sub
End Module
This program creates the result set in a List<string> just as soon as ToUpper is executed. This is an example that exhibits non-deferred execution. You have probably written code similar to this many, many times. And, of course, it produces this output:
Before using ToUpper()
Adding aaa to list
Adding Bbb to list
Adding CCc to list
After using ToUpper()
Before iterating the collection in sb
Within iteration, s: aaa
Within iteration, s: Bbb
Within iteration, s: CCc
In the previous topic, we also saw an example that used deferred execution and eager evaluation. There are a number of standard query operators that behave in this fashion, such as OrderBy. Of course, the only possible implementation of OrderBy is to iterate over the source, sort it, and then finally yield the first item in the result collection. There is no other way to do it.
So, you can have deferred execution and lazy evaluation or deferred execution and eager evaluation. Non-deferred execution is always eager.
Most of the standard query operators use deferred execution. Some of them implement lazy evaluation. In addition, most of the LINQ to XML axis methods use deferred execution and lazy evaluation.