Tag: 封闭

使用Delegates的局部变量

显然,这似乎不是最佳做法。 有人可以解释为什么它不是最佳实践或如何工作? 任何提供解释的书籍或文章将不胜感激。 //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”; } 输出的值是第二个值“已修改” 。 编译魔术的哪个部分使这个工作? 这跟跟踪堆上的值并稍后再次检索它一样简单吗? [编辑]:鉴于一些评论,改变原来的一些句子……