闭包

2017-09-18  本文已影响2人  晓龙酱
List<Action> lst = new List<Action>();
for(int i=0; i<5; i++)
{
    // wrong, output:55555
    Action act = ()=>Console.WriteLine(i);
    
    // correct, output:01234
    /*
    int temp = i;
    Action act = ()=>Console.WriteLine(temp);
    */

    lst.Add(act);
}
foreach(var fun in lst)
{
    fun();
}
上一篇 下一篇

猜你喜欢

热点阅读