The output is of course 6, because delegates capture references to outer variables.

So how is this implemented in the compiler?The compiler rewrites the code above to something like this:

Scope s = newScope();

scope.x = 5;

Action action = (scope);

scope.x = 6;

scope.Call();

and defines the Scope class like this:

privatesealedclassScope

{

publicint x;

public Scope();

publicvoid Call()

{

Console.WriteLine(this.x);

}

}

As we would expect, the is no way to create a reference to eg. an int in IL, so this is solved naturally - the int is wrapped inside Scope class and all accesses are done through the wrapper.The wrapper also defines the Call method, which is exactly the body of the anonymous delegate.

Actually, the compiler generated name for our Scope class is "<>__DisplayClass1", and the Call method is named "<Main>b__0".