可能重复:
Why is it bad to use a iteration variable in a lambda expression
C# - The foreach identifier and closures从埃里克利珀的博客: “不收,较循环变量”
从Eric Lippert's 28 June 2010项:
static IEnumerable<IEnumerable<T>>
CartesianProduct<T>(this IEnumerable<IEnumerable<T>> sequences)
{
// base case:
IEnumerable<IEnumerable<T>> result = new[] { Enumerable.Empty<T>() };
foreach(var sequence in sequences)
{
var s = sequence; // don't close over the loop variable
// recursive case: use SelectMany to build the new product out of the old one
result =
from seq in result
from item in s
select seq.Concat(new[] {item});
}
return result;
}
该var s = sequence;
看起来像一个没有操作。为什么不是一个?当sequence
直接使用时出什么问题?
而且,更主观的是:这被认为是C#行为中的一个疣?
这已被问及解释1000次之前http://stackoverflow.com/questions/227820 http://stackoverflow.com/questions/2717377 http://stackoverflow.com/questions/566687 http:// stackoverflow /问题/ 451779 http://stackoverflow.com/questions/230455 http://stackoverflow.com/questions/2951037 http://stackoverflow.com/questions/1688465 http://stackoverflow.com/questions/2242371 – 2010-07-06 22:29:11
呵呵,我不知道这件事,很高兴有一百万条关于这个的话题。 – 2010-07-07 04:03:18