What will be the output of the following code?
Most of us will expect to see the numbers 0 to 9 but in reality this code prints out ten times ten!
Why is that happening?
In the for loop there is only one variable i used and that’s incremented in each run. The delegate constructed in the loop points to the instance of variable i, not to the value. So at the end of the loop, the variable i has the value 10 and all delegates point to that.
This has to do with Captured Variables.The function Console.WriteLine(i); has to know about which i we are talking. The compiler generates a class in the background that captures the necessary variables and rewrites your code for you to make all the magic happen.
Here is an example that shows what the compiler is doing for us:
static void Main(string args)
public class Capture
Here you can clearly see why the value of name has changed when calling Print();
The thing to remember is that ‘Closures close over variables, not over values’.
Currently, in C# 4 this behavior is also happening with the foreach statement. However, C# 5 will make the breaking change of making sure that the loop variable will be inside the loop!