可能重複:
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