C# Closures, why is the loopvariable captured by reference?

前端 未结 6 1535
南笙
南笙 2020-11-30 11:39

In this example, I\'m attempting to pass by value, but the reference is passed instead.

for (int i = 0; i < 10; i++)
{
    Thread t = new Thread(() =>         


        
6条回答
  •  生来不讨喜
    2020-11-30 12:13

    Short answer: closures. Long answer given here (among other places): Differing behavior when starting a thread: ParameterizedThreadStart vs. Anonymous Delegate. Why does it matter?

提交回复
热议问题