C# 无法确定条件表达式的类型,因为 'int' 和 <null> 之间没有隐式转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18260528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:42:55  来源:igfitidea点击:

Type of conditional expression cannot be determined because there is no implicit conversion between 'int' and <null>

c#nullable

提问by davidhq

Why does this not compile?

为什么这不能编译?

int? number = true ? 5 : null;

Type of conditional expression cannot be determined because there is no implicit conversion between 'int' and <null>

无法确定条件表达式的类型,因为 'int' 和 <null> 之间没有隐式转换

采纳答案by jason

The spec (§7.14) says that for conditional expression b ? x : y, there are three possibilities, either xand yboth have a type andcertain good conditionsare met, only one of xand yhas a type andcertain good conditionsare met, or a compile-time error occurs. Here, "certain good conditions" means certain conversions are possible, which we will get into the details of below.

该规范(§7.14)说,对于条件表达式b ? x : y,有三种可能,要么xy都有一个类型某些良好的条件得到满足,只有一个xy有型某些良好的条件得到满足,或者编译时错误发生。在这里,“某些良好的条件”意味着某些转换是可能的,我们将在下面详细介绍。

Now, let's turn to the germane part of the spec:

现在,让我们转向规范的相关部分:

If only one of xand yhas a type, and both xand yare implicitly convertible to that type, then that is the type of the conditional expression.

如果只有一个xy具有类型,都xy是隐式转换为这种类型,那么这是条件表达式的类型。

The issue here is that in

这里的问题是在

int? number = true ? 5 : null;

only one of the conditional results has a type. Here xis an intliteral, and yis nullwhich does nothave a type and nullis not implicitly convertible to an int1. Therefore, "certain good conditions" aren't met, and a compile-time error occurs.

只有一个条件结果具有类型。这里x是一个int文字,并且ynull其中没有具有类型null不隐式转换为一个int1。因此,不满足“某些良好条件”,并发生编译时错误。

There aretwo ways around this:

解决此两种方法:

int? number = true ? (int?)5 : null;

Here we are still in the case where only one of xand yhas a type. Note that nullstilldoes not have a type yet the compiler won't have any problem with this because (int?)5and nullare both implicitly convertible to int?(§6.1.4 and §6.1.5).

在这里,我们仍然处于只有其中一个xy具有类型的情况。请注意,null仍然没有类型,但编译器不会对此有任何问题,因为(int?)5null都可以隐式转换为int?(第 6.1.4 节和第 6.1.5 节)。

The other way is obviously:

另一种方式显然是:

int? number = true ? 5 : (int?)null;

but now we have to read a differentclause in the spec to understand why this is okay:

但是现在我们必须阅读规范中的不同条款才能理解为什么这是可以的:

If xhas type Xand yhas type Ythen

  • If an implicit conversion (§6.1) exists from Xto Y, but not from Yto X, then Yis the type of the conditional expression.

  • If an implicit conversion (§6.1) exists from Yto X, but not from Xto Y, then Xis the type of the conditional expression.

  • Otherwise, no expression type can be determined, and a compile-time error occurs.

如果x有类型X并且y有类型Y那么

  • 如果隐式转换(第 6.1 节)存在 from Xto Y,但不存在 from Yto X,则Y是条件表达式的类型。

  • 如果隐式转换(第 6.1 节)存在 from Yto X,但不存在 from Xto Y,则X是条件表达式的类型。

  • 否则,无法确定表达式类型,并发生编译时错误。

Here xis of type intand yis of type int?. There is no implicit conversion from int?to int, but there is an implicit conversion from intto int?so the type of the expression is int?.

这里x是 typeint并且y是 type int?。没有从int?to 的隐式转换int,但是有一个从intto的隐式转换,int?所以表达式的类型是int?

1: Note further that the type of the left-hand side is ignored in determining the type of the conditional expression, a common source of confusion here.

1:进一步注意,在确定条件表达式的类型时忽略了左侧的类型,这是此处常见的混淆来源。

回答by Marc Gravell

nulldoes not have any identifiable type - it just needs a little prodding to make it happy:

null没有任何可识别的类型——它只需要一点刺激就可以让它开心:

int? number = true ? 5 : (int?)null;

回答by Andrew

As others have mentioned, the 5 is an int, and nullcannot be implicitly converted to int.

正如其他人所提到的, 5 是一个int,并且null不能隐式转换为int

Here are other ways to work around the issue:

以下是解决此问题的其他方法:

int? num = true ? 5 : default(int?);
int? num = true ? 5 : new int?();

int? num = true ? 5 : null as int?;
int? num = true ? 5 : (int?)null;

int? num = true ? (int?)5 : null;
int? num = true ? 5 as int? : null;

int? num = true ? new int?(5) : null;

Also, anywhere you see int?, you could also use Nullable<int>.

此外,在您看到的任何地方int?,您也可以使用Nullable<int>.

回答by WBuck

In C# 9this is now allowed blog

C# 9这现在允许的博客

Target typed ?? and ?

Sometimes conditional ?? and ?: expressions don't have an obvious shared type between the branches. Such cases fail today, but C# 9.0 will allow them if there's a target type that both branches convert to:

目标类型 ?? 和 ?

有时有条件??和 ?: 表达式在分支之间没有明显的共享类型。这种情况今天会失败,但如果存在两个分支都转换为的目标类型,C# 9.0 将允许它们:

Person person = student ?? customer; // Shared base type
int? result = b ? 0 : null; // nullable value type

Or your example:

或者你的例子:

// Allowed in C# 9.
int? number = true ? 5 : null;