What is the use of decorator sequences in deferred execution?

I am learning about LINQ, and trying to understand, how deferred execution works?

The single line that is bothering me is :-

Query operators provide deferred execution by returning decorator sequences.

I have tried searching about decorators, and the information I got was that:-

Decorators attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.

I am not able to make any link b/w the execution of LINQ(Deferred execution) and the role of decorators there.

Therefore, I just want to know the role of decorator/decorator sequences in LINQ's deferred execution.

Jon Skeet
people
quotationmark

I won't answer for the actual terminology used, but the guts to understand is that LINQ to Objects is effectively implemented using iterator blocks (whether or not that's the actual implementation is somewhat irrelevant). So for example, ignoring argument validation, consider Where:

public static IEnumerable<T> Where(this IEnumerable<T> source,
                                   Func<T, bool> predicate)
{
    // TODO: Eager argument validation (not as easy as it sounds)
    foreach (var item in source)
    {
        if (predicate(item))
        {
            yield return item;
        }
    }
}

What's important is that this code only asks the original input source for its data when the sequence returned is asked for the next element. That's deferred execution for you. In fact, none of this method executes until the caller asks calls GetEnumerator() and then MoveNext().

This is a decorator in that the returned sequence effectively ends up remembering a reference to the original sequence, and just performing a transformation on it as it goes along, rather than immediately fetching all the items.

For further information on this, see:

people

See more on this question at Stackoverflow