I have .net 4.5 installed on my box and I understand that below behavior is related to difference in the way foreach captures closure between .net 3.5 and 4.
I'd like to better understand why running the same code in both VS 2012 and then with VS2010 shows different output when I compiling using .net Framework 4 in both cases, is it that VS 2012 runs csc with some special flag that cause it to capture closure?
var words = new[] { "foo", "bar", "baz", "beer" };
var actions = new List<Action>();
foreach (string word in words)
{
actions.Add(() => Console.WriteLine(word));
}
actions.ForEach(e => e());
Output: With VS2010 and .net 4, above program yields "beer" 4 times
With VS2012 and .net 4:
foo bar baz beer