Category Archives: linq

March, 2012

LINQ foreach iteration variable capturing and how it’s fixed in Visual Studio 11

Cause closures are confusing

A while a go, the C# community got surprised by another GOTCHA, discovering that using a foreach iteration variable inside a LINQ query, may not always yield the expected results. The variable gets captured by the foreach loop and, due to the evil work of closure, it remains scoped outside of the LINQ query itself living its own life so to speak. This has now been corrected in the Visual Studio 11 Beta.

Continue reading