4th*_*ace 3 .net c# closures function anonymous-methods
testit()方法是一个闭包.aString已超出范围,但testit()仍然可以在其上执行.testit2()使用的是一个没有超出范围(mystring)的变量,但是它也没有传递给testit2().testit2()被认为是封闭吗?
string mystring = "hello world";
Action testit = new Action(delegate { string aString = "in anon method"; Debug.WriteLine(aString); });
testit();
//capture mystring. Is this still a closure?
Action testit2 = new Action(delegate { Debug.WriteLine(mystring); });
//mystring is still in scope
testit2();
Run Code Online (Sandbox Code Playgroud)
在第二个示例中,mystring可以在方法之外更新,这些更改将反映在testit2()中.这不像普通方法,只能捕获mystring作为参数.
这两个例子都是匿名函数.第二个使用闭包来捕获局部变量.变量相对于匿名函数的范围生命周期不会影响是否创建闭包以使用它.只要变量在匿名函数之外定义并在其中使用,就会创建一个闭包.
第一个匿名函数不使用本地状态,因此不需要闭包.它应该编译成静态方法.
这是必要的,因为匿名函数可以超过当前函数的生命周期.因此,必须捕获匿名函数中使用的所有本地,以便稍后执行委托.
| 归档时间: |
|
| 查看次数: |
398 次 |
| 最近记录: |