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();
}
闭包
最新推荐文章于 2024-02-04 15:49:23 发布