Sorry it's a little dumb...
List<Action> ps = new List<Action>();
for (int i = 0; i < 10; i++)
{
var k = i;
ps.Add(() => Console.WriteLine($"print {k}"));
}
for(int i=0;i<10;i++)
{
ps[i].Invoke();
}
// 0 1 2 ... 9
List<Action> ps = new List<Action>();
for (int i = 0; i < 10; i++)
{
ps.Add(() => Console.WriteLine($"print {i}"));
}
for(int i=0;i<10;i++)
{
ps[i].Invoke();
}
// 10 10 10 ... 10
Why do i have to pass a new variable k to make the delegate work?