Skip to main content
← Back to Code Arena
Medium Engineer SpeedRun linq deferred-execution closures

LINQ: The Deferred Execution Trap

+200 XP · 6 min · attempt 1

Problem

What does this snippet print?

    var nums = new List<int> { 1, 2, 3 };
    var query = nums.Where(n => n > threshold);
    int threshold = 0;
    threshold = 2;
    nums.Add(4);
    threshold = 1;
    Console.WriteLine(string.Join(",", query));

Assume `threshold` is declared before the Where call (the order shown is the assignment order). Enter the exact output (e.g. `2,3,4`).

Starter

// LINQ Where doesn't run when you call it.
// It runs when you iterate (string.Join enumerates).

Your answer

Playing as a guest — you'll be asked for a display name on submit. Log in to keep your XP across devices.