UPDATE: We are taking the breaking change. In C# 5, the loop variable of a
foreach will be logically inside the loop, and therefore closures will close over a fresh copy of the variable each time. The
for loop will not be changed. We return you now to our original article.
I don’t know why I haven’t blogged about this one before; this is the single most common incorrect bug report we get. That is, someone thinks they have found a bug in the compiler, but in fact the compiler is correct and their code is wrong. That’s a terrible situation for everyone; we very much wish to design a language which does not have “gotcha” features like this.
But I’m getting ahead of myself. Continue reading