Captured variable in a loop in C#
Yes – take a copy of the variable inside the loop: while (variable < 5) { int copy = variable; actions.Add(() => copy * 2); ++ variable; } You can think of it as if the C# compiler creates a “new” local variable every time it hits the variable declaration. In fact it’ll create appropriate … Read more