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
Type of conditional expression cannot be determined because there is no implicit conversion between 'int' and <null>
提问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 x
and y
both have a type andcertain good conditionsare met, only one of x
and y
has 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
,有三种可能,要么x
和y
都有一个类型和某些良好的条件得到满足,只有一个x
和y
有型和某些良好的条件得到满足,或者编译时错误发生。在这里,“某些良好的条件”意味着某些转换是可能的,我们将在下面详细介绍。
Now, let's turn to the germane part of the spec:
现在,让我们转向规范的相关部分:
If only one of
x
andy
has a type, and bothx
andy
are implicitly convertible to that type, then that is the type of the conditional expression.
如果只有一个
x
与y
具有类型,都x
和y
是隐式转换为这种类型,那么这是条件表达式的类型。
The issue here is that in
这里的问题是在
int? number = true ? 5 : null;
only one of the conditional results has a type. Here x
is an int
literal, and y
is null
which does nothave a type and null
is not implicitly convertible to an int
1. Therefore, "certain good conditions" aren't met, and a compile-time error occurs.
只有一个条件结果具有类型。这里x
是一个int
文字,并且y
是null
其中没有不具有类型和null
不隐式转换为一个int
1。因此,不满足“某些良好条件”,并发生编译时错误。
There aretwo ways around this:
有是解决此两种方法:
int? number = true ? (int?)5 : null;
Here we are still in the case where only one of x
and y
has a type. Note that null
stilldoes not have a type yet the compiler won't have any problem with this because (int?)5
and null
are both implicitly convertible to int?
(§6.1.4 and §6.1.5).
在这里,我们仍然处于只有其中一个x
和y
具有类型的情况。请注意,null
仍然没有类型,但编译器不会对此有任何问题,因为(int?)5
和null
都可以隐式转换为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
x
has typeX
andy
has typeY
then
If an implicit conversion (§6.1) exists from
X
toY
, but not fromY
toX
, thenY
is the type of the conditional expression.If an implicit conversion (§6.1) exists from
Y
toX
, but not fromX
toY
, thenX
is 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
X
toY
,但不存在 fromY
toX
,则Y
是条件表达式的类型。如果隐式转换(第 6.1 节)存在 from
Y
toX
,但不存在 fromX
toY
,则X
是条件表达式的类型。否则,无法确定表达式类型,并发生编译时错误。
Here x
is of type int
and y
is of type int?
. There is no implicit conversion from int?
to int
, but there is an implicit conversion from int
to int?
so the type of the expression is int?
.
这里x
是 typeint
并且y
是 type int?
。没有从int?
to 的隐式转换int
,但是有一个从int
to的隐式转换,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
null
does 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 null
cannot 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# 9
this 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;