Consider this code.
var values = new List<int> {123, 432, 768}; var funcs = new List<Func<int>>(); values.ForEach(v=>funcs.Add(()=>v)); funcs.ForEach(f=>Console.WriteLine(f()));//prints 123,432,768 funcs.Clear(); foreach (var v1 in values) { funcs.Add(()=>v1); } foreach (var func in funcs) { Console.WriteLine(func()); //prints 768,768,768 } I know that the second foreach prints 768 3 times because of the closure variable captured by the lambda. why does it not happen in the first case?How does foreach keyword different from the method Foreach? Is it beacuse the expression is evaluated when i do values.ForEach