循环中的 Lambda 变量捕获 - 这里发生了什么? [英] Lambda variable capture in loop - what happens here?
问题描述
我正在努力解决问题,这里发生了什么?编译器生成什么样的代码?
I am trying to get my head around, what happens here ? What sort of code does the compiler produce?
public static void vc()
{
var listActions = new List<Action>();
foreach (int i in Enumerable.Range(1, 10))
{
listActions.Add(() => Console.WriteLine(i));
}
foreach (Action action in listActions)
{
action();
}
}
static void Main(string[] args)
{
vc();
}
输出:1010..10
根据this,ActionHelper 的新实例将是为每次迭代创建.所以在那种情况下,我认为它应该打印 1..10.有人能给我一些关于编译器在这里做什么的伪代码吗?
According to this, a new instance of ActionHelper would be created for every iteration. So in that case, I would assume it should print 1..10. Can someone give me some pseudo code of what the compiler is doing here ?
谢谢.
推荐答案
在这一行
listActions.Add(() => Console.WriteLine(i));
变量 i
被捕获,或者,如果您愿意,可以创建一个 指针 到该变量的内存位置.这意味着每个委托都有一个指向该内存位置的指针.在这个循环执行之后:
the variable i
, is captured, or if you wish, created a pointer to the memory location of that variable. That means that every delegate got a pointer to that memory location. After this loop execution:
foreach (int i in Enumerable.Range(1, 10))
{
listActions.Add(() => Console.WriteLine(i));
}
由于显而易见的原因i
是10
,所以Action
(s)中存在的所有指针指向的内存内容变为10.
for obvious reasons i
is 10
, so the memory content that all pointers present in Action
(s) are pointing, becomes 10.
换句话说,i
被捕获了.
In other words, i
is captured.
顺便说一下,应该注意的是,根据 Eric Lippert 的说法,这种奇怪"的行为将在 C# 5.0
中解决.
By the way, should note, that according to Eric Lippert, this "strange" behaviour would be resolved in C# 5.0
.
因此在 C# 5.0
中,您的程序将按预期打印:
So in the C# 5.0
your program would print as expected:
1,2,3,4,5...10
找不到埃里克·利珀特 (Eric Lippert) 关于主题的帖子,但这里有另一个:
Can not find Eric Lippert's post on subject, but here is another one:
这篇关于循环中的 Lambda 变量捕获 - 这里发生了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!