-2
为什么代码不能编译,当它做同样的事情时。为什么代码不能编译
错误消息:
无法从System.Action转换为System.Threading.ThreadStart。
代码:
// Compiles and works
for (int i = 0; i < 100000; i++)
{
Thread t = new Thread(() => {
Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(100); Interlocked.Increment(ref Count);
});
t.Start();
}
// doesn't compile
Action action =() => {
Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(100);
Interlocked.Increment(ref Count);
};
for (int i = 0; i < 100000; i++)
{
Thread t = new Thread(action);
}
你会得到什么错误? –
无法从System.Action转换为System.Threading.ThreadStart,但不是第一个做同样的事情 – loneshark99
我问的是,为什么编译器不知道它具有相同的签名,也就是说它没有使用任何参数,不返回任何东西。不要这样做,它隐含地像前一个 – loneshark99