关于 foreach 和委托的问题 [英] Question about foreach and delegates

查看:38
本文介绍了关于 foreach 和委托的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设如下代码:

foreach(Item i on ItemCollection)
{
   Something s = new Something();
   s.EventX += delegate { ProcessItem(i); };
   SomethingCollection.Add(s);
}

当然,这是错误的,因为所有的代表都指向同一个Item.替代方案是:

Of course, this is wrong because all the delegates points to the same Item. The alternative is:

foreach(Item i on ItemCollection)
{
   Item tmpItem = i;
   Something s = new Something();
   s.EventX += delegate { ProcessItem(tmpItem); };
   SomethingCollection.Add(s);
}

在这种情况下,所有代表都指向自己的项目.

In this case all the delegates point to their own Item.

这种方法怎么样?还有其他更好的解决方案吗?

What about this approach? There is any other better solution?

推荐答案

更新:这里有关于这个问题的广泛分析和评论:

UPDATE: There is extensive analysis and commentary on this issue here:

http://ericlippert.com/2009/11/12/close-over-the-loop-variable-thinked-harmful-part-one/

这是一个非常频繁报告的问题;通常它被报告为编译器错误,但实际上编译器根据规范做了正确的事情.匿名函数关闭变量,而不是,并且只有一个foreach循环变量.因此,每个 lambda 都会关闭同一个变量,从而获得该变量的当前值.

This is an extremely frequently reported issue; usually it is reported as a compiler bug, but in fact the compiler is doing the right thing according to the specification. Anonymous functions close over variables, not values, and there is only a single foreach loop variable. Therefore each lambda closes over the same variable, and therefore gets the current value of that variable.

这几乎让每个人都感到惊讶,并导致很多混乱和许多错误报告.我们正在考虑更改 C# 假设​​未来版本的规范和实现,以便在循环结构中逻辑声明循环变量,从而在每次循环时提供一个新鲜"变量.

This is surprising to almost everyone, and leads to much confusion and many bug reports. We are considering changing the specification and implementation for a hypothetical future version of C# so that the loop variable is logically declared inside the looping construct, giving a "fresh" variable every time through the loop.

这将是一个重大变化,但我怀疑依赖这种奇怪行为的人数很少.如果您对此主题有意见,请随时向上述更新中提到的博客文章添加评论.谢谢!

This would be a breaking change, but I suspect the number of people who depend on this strange behaviour is quite low. If you have opinions on this subject, feel free to add comments to the blog posts mentioned up the update above. Thanks!

这篇关于关于 foreach 和委托的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆