int[] data = new int[] { 1, 2, 3, 4, 5 };
List<Func<int>> actions = new List<Func<int>>();IEnumerator e = data.GetEnumerator();int x = 0;while (e.MoveNext()){ x = (int)e.Current; actions.Add(() => x);}foreach (var foo in actions){ Console.WriteLine(foo());}
运行结果为什么是55555,能不能把第二个foreach改成 while然我看看
这是闭包的一个陷阱 参考 http://www.2cto.com/kf/201405/297249.html javascript也有类似问题
可以使用reflector反编译一下编译好的库,这样就可以知道底层的运作原理了
2012 引用的文章将的很清楚。
使用reflector反编译一下编译好的库,这样就可以知道底层的运作原理了
请问怎么反编译?能不能给我解释一下
http://q.cnblogs.com/q/69086/
闭包
如果你不想闭包,请将变量x声明在while循环体内,code 如下:
while (e.MoveNext()) { int x = (int)e.Current; actions.Add(() => x); }
1L正解