Where does anonymous function body variables saved ?
问题 Below code is working but why ? Where does the x and y coming/saved when I invoke anonymous method in the loop. thanks static void Main(string[] args) { int x=1; int y=2; var dic = GetDic(x, y); for (int i = 0; i < 5;i++ ) { System.Console.WriteLine(dic[i].Invoke().ToString()); } } private static Dictionary<int, Func<int>> GetDic(int x, int y) { var dic = new Dictionary<int, Func<int>>() { {0,()=>{return y;}}, {1,()=>{return x;}}, {2,()=>{return x+y;}}, {3,()=>{return x-y;}}, {4,()=>{return y