Skip to content
Sharp

Delegates & events · interview question

What does this program print?

What does this code output?Hard
C#
var actions = new List<Action>();
for (int i = 0; i < 3; i++)
{
    actions.Add(() => Console.Write(i));
}
foreach (var action in actions) action();
Choose one answer
Answer and explanation

Correct answer

A. 333

Why

Lambdas capture the variable, not its value. A for loop has one i shared by all iterations; when the actions run, i is 3. Fix it with int copy = i; inside the loop and capture copy.

Test yourself on Delegates & events

A free test with instant, explained results. No account needed.

Start free test