在将方法分配给Func
-type时,我得到了编译错误Type of conditional expression cannot be determined because there is no implicit conversion between 'method group' and 'method group'
。
这只发生在? :
运算符上。代码:
public class Test
{
public static string One(int value)
{
value += 1;
return value.ToString();
}
public static string Two(int value)
{
value += 2;
return value.ToString();
}
public void Testing(bool which)
{
// This works
Func<int, string> actionWorks;
if (which) actionWorks = One; else actionWorks = Two;
// Compilation error on the part "One : Two"
Func<int, string> action = which ? One : Two;
}
}
我找到了关于协变和逆变的some information,但我看不出这如何适用于上面的情况。为什么这个不起作用?
https://stackoverflow.com/questions/6308328
复制相似问题