currentValue is no longer a local variable: it is a captured variable. This compiles to something like:
class Foo {
public string currentValue; // yes, it is a field
public void SomeMethod(object sender, EventArgs e) {
Response.Write(currentValue);
}
}
...
public Page_Index() {
Foo foo = new Foo();
foo.currentValue = "This is the FIRST value";
this.Load += foo.SomeMethod;
foo.currentValue = "This is the MODIFIED value";
}
Jon Skeet has a really good write up of this in C# in Depth, and a separate (not as detailed) discussion here.
Note that the variable currentValue is now on the heap, not the stack - this has lots of implications, not least that it can now be used by various callers.
This is different to java: in java the value of a variable is captured. In C#, the variable itself is captured.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…