操作'?“不能应用到子类类型的操作数 [英] Operator '??' cannot be applied to operands of type for child classes

查看:97
本文介绍了操作'?“不能应用到子类类型的操作数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码给出了在主函数中的第二行标题错误。

The following code gives the error in the title on the second line in the Main function.

public class P {}

public class B : P {}

public class A : P {}

void Main()
{   
    P p = GetA()??GetB();
}

public A GetA() 
{
    return new A();
}

public B GetB()
{
    return new B();
}

一个简单的拧紧,这样的

A simple tweak to the line like these

    p = (P)GetA()??GetB();
    or
    p = GetA()??(P)GetB();



工作

works.

我很好奇为什么编译器不明白,都是左侧容器的子类,并允许操作,而投?

I'm curious why the compiler doesn't understand that both are child classes of the left hand side container and allow the operation without the cast?

推荐答案

类型在左手侧的说法必须与在右手侧,或反之亦然的类型兼容。换句话说,必须存在从 B 的隐式转换,或从 A <到 A / code>到 B

The type of the argument on the left hand side must be compatible with the type on the right hand side or vice versa. In other words, there must exist an implicit conversion from B to A or from A to B.

var a = x ?? y;

在上面的,如果从ÿ的隐式转换 X 然后 X 成为表达的类型。如果从 X 的隐式转换,但是从的隐式转换X ,那么的类型将成为表达式的类型。如果没有转换存在于两个方向,繁荣;编译错误。从规格:

In the above, if there is an implicit conversion from y to x then the type of x becomes the type of the expression. if there is no implicit conversion from y to x, but there is an implicit conversion from x to y, then the type of y becomes the type of the expression. if no conversion exists in either direction, boom; compilation error. From the spec:

类型的表达式的? B依赖于它的隐式转换类型操作数之间可用。在优先顺序,a的类型? b为A0,A,或B,其中A是一个类型,B是B类型(前提是b有一个类型)和A0是基础型的,如果A是一个可空类型或以其他方式。具体地,??

The type of the expression a ?? b depends on which implicit conversions are available between the types of the operands. In order of preference, the type of a ?? b is A0, A, or B, where A is the type of a, B is the type of b (provided that b has a type), and A0 is the underlying type of A if A is a nullable type, or A otherwise. Specifically, a ?? b is processed as follows:

•如果A不是可空类型或引用类型,编译时出现错误

• If A is not a nullable type or a reference type, a compile-time error occurs.

•如果A是一个可空类型和隐式转换从b到A0存在,结果类型为A0。在运行时,首先计算。如果不为空,则a解包为类型A0,这即是结果。否则,计算b并转换为类型A0,这即是结果。

• If A is a nullable type and an implicit conversion exists from b to A0, the result type is A0. At run-time, a is first evaluated. If a is not null, a is unwrapped to type A0, and this becomes the result. Otherwise, b is evaluated and converted to type A0, and this becomes the result.

•否则,如果隐式转换从B到A的存在,结果类型为A 。在运行时,首先计算。如果不为空,一个变成了结果。否则,计算b并转换为A型,而这即是结果。

• Otherwise, if an implicit conversion exists from b to A, the result type is A. At run-time, a is first evaluated. If a is not null, a becomes the result. Otherwise, b is evaluated and converted to type A, and this becomes the result.

•否则,如果B有B型和隐式转换存在从A0到B ,结果类型为B.在运行时,首先计算。如果不为空,则a解包为类型A0(除非A和A0是同一类型),并转换为B型,而这即是结果。否则,计算b并成为结果。

• Otherwise, if b has a type B and an implicit conversion exists from A0 to B, the result type is B. At run-time, a is first evaluated. If a is not null, a is unwrapped to type A0 (unless A and A0 are the same type) and converted to type B, and this becomes the result. Otherwise, b is evaluated and becomes the result.

•否则,a和b不兼容,并发生编译时错误。

• Otherwise, a and b are incompatible, and a compile-time error occurs.

这篇关于操作'?“不能应用到子类类型的操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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