Skip to main content
Journey Uncommon Logo
JourneyUncommon
mediumC#delegates Func ActionSingle-choice MCQ

Why does this loop print '3,3,3' rather than '0,1,2' on C# versions before 5.0 (and what fixed it)?

var actions = new List<Action>(); for (int i = 0; i < 3; i++) actions.Add(() => Console.Write(i)); foreach (var a in actions) a(); // legacy for-loop, single shared i