我想围绕我的头,这里会发生什么?什么样的code没有编译器产生?
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();
}
输出:的 10 10 .. 10
output: 10 10 .. 10
据this, ActionHelper的新实例将为每个迭代被创建。因此,在这种情况下,我会假设它应该打印1..10。 有人可以给我什么样的编译器在这里做一些伪code?
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));
变量我
,被捕获,或者如果你愿意的话,创建一个指针以该变量的存储位置。这意味着,每委托有一个的指针的该内存位置。这个循环执行后:
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));
}
由于显而易见的原因我
是 10
,所以内存的内容,所有的指针present在动作
(S)被指指点点,变为10。
for obvious reasons i
is 10
, so the memory content that all pointers present in Action
(s) are pointing, becomes 10.
在换句话说,我
被抓获。
顺便说一句,应该注意,根据埃里克利珀,这种奇怪的行为将是解决在 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
编辑:
找不到埃里克利珀对主题的帖子,但这里是另一个问题:
Can not find Eric Lippert's post on subject, but here is another one:
封闭的循环再探