条件语句中,泛型委托不必要强制转换 [英] Conditional statement, generic delegate unnecessary cast

查看:169
本文介绍了条件语句中,泛型委托不必要强制转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当设置 Action< T> 值时,我遇到了一个带有条件语句的奇怪问题。这并不是说我不知道​​如何解决这个问题,因为如果使用普通的,如果很容易解决。



这是我的问题:

  public class Test 
{
public bool Foo {get;组; }
public Action< bool>行动{get;组; }

public void A()
{
Action = Foo? B:C; //给出编译器错误
}

public void B(bool value)
{

}

public void C(bool value)
{

}
}

这给了我一个编译错误信息


'method group'和'method group之间没有隐式转换'。

这很奇怪,因为我不明白为什么这是非法的。



顺便说一下,下面的语法将使这个有效的(从编译器的角度来看):

  public void A()
{
Action = Foo? (动作< bool>)B:C;

$ / code>

所以也许你可以阅读这个问题,为什么必要的演员? / p>

解决方案

您正在混淆两个相似的概念:

A)A方法组。方法组是一个或多个具有相同名称的C#方法。这是一个主要由编译器使用的抽象;你不能传递一个方法组。你可以用方法组完成所有的工作,就是调用它或者创建一个委托。如果类型签名匹配,您可以隐式地从方法组创建一个委托。



B)委托。你知道一个代表是什么;它有一个特定的类型签名,并直接指向一种方法。



所以在第一个例子中,你的表达式返回一个方法组<$一边是c $ c> B ,另一边是另一个方法组 C 。三元运算符需要在两边都返回相同的类型,但它不知道向哪一方投掷什么;将结果赋给( Action< bool> )的变量类型不会确定表达式的类型。在第二个例子中,您合法地将方法组 B 转换为在三元运算符的一侧使用< bool> 委托。在试图消除表达歧义的过程中,编译器试图将每一边都转换为另一边的类型。它可以成功地将方法组 C 转换为 Action< bool> ,所以它是这样做的,并且表达式是合法的。

I'm having this really weird problem with a conditional statement when setting an Action<T> value. It's not that I don't know how to work around this as it's pretty easy to solve by using a normal if.

Here's my problem:

public class Test
{
    public bool Foo { get; set; }
    public Action<bool> Action { get; set; }

    public void A()
    {
        Action = Foo ? B : C;//Gives compiler error
    }

    public void B(bool value)
    {

    }

    public void C(bool value)
    {

    }
}

This gives me a compiler error with the message

There's no implicit conversion between 'method group' and 'method group'.

Which is strange as I can't figure out why this would be illegal.

By the way, the below syntax will make this valid (from the compilers point of view):

    public void A()
    {
        Action = Foo ? (Action<bool>) B : C;
    }

So maybe you can read the question as, why is the cast necessary?

解决方案

You're conflating two similar concepts:

A) A method group. A method group is one or more C# methods with the same name. It's an abstraction used primarily by the compiler; you can't pass around a method group. All you can do with a method group is invoke it or create a delegate out of it. You can implicitly create a delegate from a method group if the type signatures match.

B) A delegate. You know what a delegate is; it has a specific type signature and refers directly to a method. As well as invoking it, you can pass it around and treat it as a first-class object.

So in the first example, your expression returns a method group B on the one side and another method group C on the other side. The ternary operator needs to return the same type on both sides, but it doesn't know what to cast either side to; the variable type you assigned the result to (Action<bool>) doesn't determine the type of the expression. So it's ambiguous.

In the second example, you legally cast the method group B to an Action<bool> delegate on the one side of the ternary operator. In the process of trying to disambiguate the expression, the compiler tries to cast each side to the type of the other side. It can successfully cast method group C to an Action<bool>, so it does so and the expression is legal.

这篇关于条件语句中,泛型委托不必要强制转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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