C# 带有委托的局部变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/148669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Local variables with Delegates
提问by Hugoware
This is clearly notappears like it wouldn't be a best practice. Can someone explain why it would not be a best practice or how this works? Any books or articles providing an explanation would be appreciated.
这显然不是最佳实践。有人可以解释为什么这不是最佳实践或它是如何工作的吗?任何提供解释的书籍或文章将不胜感激。
//The constructor
public Page_Index() {
//create a local value
string currentValue = "This is the FIRST value";
//use the local variable in a delegate that fires later
this.Load += delegate(object sender, EventArgs e) {
Response.Write(currentValue);
};
//change it again
currentValue = "This is the MODIFIED value";
}
The value that is output is the second value "Modified". What part of the compiler magic is making this work? Is this as simple as keeping track of the value on the heap and retrieving it again later?
输出的值是第二个值"Modified"。编译器魔术的哪一部分使这项工作起作用?这是否像跟踪堆上的值并稍后再次检索它一样简单?
[Edit]: Given some of the comments, changing the original sentence some...
[编辑]:鉴于一些评论,将原始句子更改一些...
采纳答案by Marc Gravell
currentValue is no longer a local variable: it is a capturedvariable. This compiles to something like:
currentValue 不再是一个局部变量:它是一个捕获的变量。这编译为类似:
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.
Jon Skeet 在C# 中深入地对此进行了非常好的描述,并在此处进行了单独的(不是那么详细)讨论。
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.
请注意,变量 currentValue 现在在堆上,而不是堆栈上——这有很多含义,尤其是现在它可以被各种调用者使用。
This is different to java: in java the valueof a variable is captured. In C#, the variable itselfis captured.
这与 java 不同:在 java 中,变量的值被捕获。在 C# 中,变量本身被捕获。
回答by leppie
You need to capture the value of the variable within the closure/delegate, else it can be modified, like you saw.
您需要在闭包/委托中捕获变量的值,否则可以修改它,就像您看到的那样。
Assign currentValue to a variable local (inside) to the delegate.
将 currentValue 分配给委托的本地(内部)变量。
回答by Marc Gravell
I suppose more the question I am asking is that how is it working with a local variable [MG edit: "Ack - ignore this..." was added afterwards]
我想我要问的更多问题是它如何与局部变量一起工作 [MG 编辑:“确认 - 忽略这个......”是后来添加的]
That is the point; it really isn'ta local variable any more - at least, not in terms of how we normally think of them (on the stack etc). It looks like one, but it isn't.
这就是我想说的; 它真的不再是一个局部变量——至少,不是我们通常对它们的看法(在堆栈上等)。它看起来像一个,但它不是。
And for info, re "not good practice" - anonymous methods and captured variables are actually an incredibly powerful tool, especiallywhen working with events. Feel free to use them, but if you are going down this route, I would recommend picking up Jon's book to make sure you understand what is actually happening.
对于信息,重新“不好的做法” - 匿名方法和捕获的变量实际上是一个非常强大的工具,尤其是在处理事件时。随意使用它们,但如果您要沿着这条路走下去,我建议您拿起 Jon 的书,以确保您了解实际发生的情况。