Deferred Execution

noun

say it: “dih-FERD ek-sih-KYOO-shun” /dɪˈfɜːrd ˌɛksɪˈkjuːʃən/

A LINQ query running only when its results are used, not when it is written.

What is Deferred Execution in C#?

Deferred Execution — A LINQ query isn't run when you write it, only when you use its results: in a foreach, or with ToList() or Count(). Change the list in between and the query sees the change.

List<int> scores = [21, 14];
IEnumerable<int> high = scores.Where(s => s > 20); // nothing runs yet
scores.Add(35);
Console.WriteLine(high.Count()); // 2

In a sentence

  • “Because of deferred execution, the query saw the score I added after writing it.”

Level: Freelancer · More C# terms at this level

Updated