首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >无法确定条件表达式的类型(Func)

无法确定条件表达式的类型(Func)
EN

Stack Overflow用户
提问于 2011-06-10 23:11:38
回答 3查看 5.9K关注 0票数 22

在将方法分配给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,但我看不出这如何适用于上面的情况。为什么这个不起作用?

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6308328

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档