为什么" Func键<布尔>测试=价值? F:F"不能编译? [英] Why does "Func<bool> test = value ? F: F" not compile?

查看:75
本文介绍了为什么" Func键<布尔>测试=价值? F:F"不能编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了这个类似的问题,但它们涉及不同类型的,所以我认为这是一个新问题。

I have seen similar questions to this, but they involve different types so I think this is a new question.

考虑以下code:

public void Test(bool value)
{
    // The following line provokes a compiler error:
    // "Type of conditional expression cannot be determined because there is 
    // no implicit conversion between 'method group' and 'method group".

    Func<bool> test = value ? F : F;
}

public bool F()
{
    return false;
}

现在,根据C#3.0标准,

Now, according to the C# 3.0 Standard,

的第二个和第三个操作数
  ?:操作员控制的类型
  有条件的前pression。设X和Y是
  类型的第二和第三的
  操作数。然后,

The second and third operands of the ?: operator control the type of the conditional expression. Let X and Y be the types of the second and third operands. Then,

如果X和Y是相同的类型,则
  这是条件的类型
  否则,如果隐式转换
  (第6.1节)根据X存在Y,但不
  从Y到X,则Y为的类型
  有条件的前pression。否则,如果
  隐式转换(第6.1节)存在
  从Y到X,而不是从X到Y,则
  X是有条件的类型
  前pression。否则,没有前pression
  类型可被确定,并且
  编译时错误。

If X and Y are the same type, then this is the type of the conditional Otherwise, if an implicit conversion (§6.1) exists from X to Y, but not from Y to X, then Y is the type of the conditional expression. Otherwise, if an implicit conversion (§6.1) exists from Y to X, but not from X to Y, then X is the type of the conditional expression. Otherwise, no expression type can be determined, and a compile-time error occurs.

在我看来,在我的例子code,X和Y必须是同一类型的,因为它们是完全一样的实体的 Func键的。那么,为什么它不能编译?

It seems to me that in my sample code, X and Y must be of the same type, since they are the selfsame entity, Func. So why does it not compile?

推荐答案

现在的问题是显著改变,所以我原来的答复是有点过到现在。

The question was changed significantly, so my original answer is a bit off by now.

然而,问题是基本相同的。即有可能是任何号码相匹配的委托声明为˚F,并因为有两个相同的代表之间不存在隐式转换声明˚F不能转换为 Func键&LT;布尔方式&gt;

However, the problem is essentially the same. I.e. there could be any number of matching delegate declarations for F and since there is no implicit conversion between two identical delegate declarations the type of F cannot be converted to Func<bool>.

同样的,如果你声明

private delegate void X();
private delegate void Y();
private static void Foo() {}

您不能做

X x = Foo;
Y y = x;


原来的答复:


Original answer:

它不起作用,因为方法组不能被分配​​到一个隐式类型的变量。

It doesn't work because method groups cannot be assigned to an implicitly typed variable.

VAR测试= Func键; 也不起作用。

原因是,有可能是任何数量的委托类型为函数功能。例如。 函数功能匹配这两个声明的(除了动作

The reason being that there could be any number of delegate types for Func. E.g. Func matches both of these declarations (in addition to Action)

private delegate void X();
private delegate void Y();

要使用与方法组隐式类型的变量,你需要通过铸造消除不确定性。

To use implicitly typed variables with method groups, you need to remove the ambiguity by casting.

这篇关于为什么&QUOT; Func键&LT;布尔&GT;测试=价值? F:F&QUOT;不能编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆