我正在使用 Visual Studio 2012 Update 1 和 .NET 4.5 这里是代码。
void Test(Action a) { }
void Test(Func<int> a) { }
void TestError()
{
bool throwException = true;
//Resolves to Test(Action a)
Test(() =>
{
});
//Resolves to Test(Action a)
Test(() =>
{
if (throwException) throw new Exception();
});
//Resolves to Test(Func<int> a)
//(This seems like a bug since there is no return value)
Test(() =>
{
throw new Exception();
});
//Resolves to Test(Action a)
//(With warning unreachable code detected)
Test(() =>
{
throw new Exception();
return; //unreachable code detected
});
}
似乎最后一个函数调用错误地解析为 Func 而不是 Action,它与无条件抛出异常有关。
这是一个BUG吗?谢谢。