I was trying to understand the answer for this question Why am I getting wrong results when calling Func<int>? I wrote some sample code. The following code
public static void Main(string[] args) { var funcs = new List<Func<string>>(); for(int v=0,i=0;v<3;v++,i++) { funcs.Add( new Func<string>(delegate(){return "Hello "+ i++ +" "+v;}) ); } foreach(var f in funcs) Console.WriteLine(f()); } produces
Hello 3 3 Hello 4 3 Hello 5 3 After reading the explanation by Jon Skeet and Eric Lippert I thought I will get
Hello 3 3 Hello 3 3 Hello 3 3 Here both v and i are loop variables, while the value of i is picked up at that instant v is not why is this?. I don't understand the behavior.
itwice?