7
foreach标识符和闭包
在接下来的两个摘要中,第一个是安全的还是第二个必须做? 安全地说,每个线程是否保证从创建线程的同一循环迭代中调用Foo上的方法? 还是必须将引用复制到新变量“ local”到循环的每次迭代中? var threads = new List<Thread>(); foreach (Foo f in ListOfFoo) { Thread thread = new Thread(() => f.DoSomething()); threads.Add(thread); thread.Start(); } -- var threads = new List<Thread>(); foreach (Foo f in ListOfFoo) { Foo f2 = f; Thread thread = new Thread(() => f2.DoSomething()); threads.Add(thread); thread.Start(); …