为什么不能在三元if语句中将可空int设置为null ?(复制)

时间:2022-09-25 14:11:55

This question already has an answer here:

这个问题已经有了答案:

The C# code below:

下面的c#代码:

int? i;
i = (true ? null : 0);

gives me the error:

给我的错误:

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

不能确定条件表达式的类型,因为“ ”和“int”之间没有隐式转换

Shouldn't this be valid? What am i missing here?

难道这是有效的吗?我错过了什么?

3 个解决方案

#1


84  

The compiler tries to evaluate the right-hand expression. null is null and the 0 is an int literal, not int?. The compiler is trying to tell you that it can't determine what type the expression should evaluate as. There's no implicit conversion between null and int, hence the error message.

编译器尝试计算右边的表达式。null是空的,0是整型文字,不是整型?。编译器试图告诉您它不能确定表达式的值是什么类型。null和int之间没有隐式转换,因此有错误消息。

You need to tell the compiler that the expression should evaluate as an int?. There is an implicit conversion between int? and int, or between null and int?, so either of these should work:

您需要告诉编译器表达式的值应该是int?在int之间有隐式转换吗?那么int,或者在null和int之间?,所以这两种方法都应该奏效:

int? x = true ? (int?)null : 0;

int? y = true ? null : (int?)0;

#2


15  

You need to use the default() keyword rather than null when dealing with ternary operators.

在处理三元运算符时,需要使用default()关键字而不是null。

Example:

例子:

int? i = (true ? default(int?) : 0);

Alternately, you could just cast the null:

或者,你可以直接使用null

int? i = (true ? (int?)null : 0);

Personally I stick with the default() notation, it's just a preference, really. But you should ultimately stick to just one specific notation, IMHO.

就我个人而言,我坚持使用默认的()表示法,实际上这只是一种偏好。但是你最终应该只使用一个特定的符号,IMHO。

HTH!

HTH !

#3


1  

The portion (true ? null : 0) becomes a function in a way. This function needs a return type. When the compiler needs to figure out the return type it can't.

部分(真的吗?null: 0)在某种程度上成为函数。这个函数需要返回类型。当编译器需要计算返回类型时,它不能。

This works:

如此:

int? i;
i = (true ? null : (int?)0);

#1


84  

The compiler tries to evaluate the right-hand expression. null is null and the 0 is an int literal, not int?. The compiler is trying to tell you that it can't determine what type the expression should evaluate as. There's no implicit conversion between null and int, hence the error message.

编译器尝试计算右边的表达式。null是空的,0是整型文字,不是整型?。编译器试图告诉您它不能确定表达式的值是什么类型。null和int之间没有隐式转换,因此有错误消息。

You need to tell the compiler that the expression should evaluate as an int?. There is an implicit conversion between int? and int, or between null and int?, so either of these should work:

您需要告诉编译器表达式的值应该是int?在int之间有隐式转换吗?那么int,或者在null和int之间?,所以这两种方法都应该奏效:

int? x = true ? (int?)null : 0;

int? y = true ? null : (int?)0;

#2


15  

You need to use the default() keyword rather than null when dealing with ternary operators.

在处理三元运算符时,需要使用default()关键字而不是null。

Example:

例子:

int? i = (true ? default(int?) : 0);

Alternately, you could just cast the null:

或者,你可以直接使用null

int? i = (true ? (int?)null : 0);

Personally I stick with the default() notation, it's just a preference, really. But you should ultimately stick to just one specific notation, IMHO.

就我个人而言,我坚持使用默认的()表示法,实际上这只是一种偏好。但是你最终应该只使用一个特定的符号,IMHO。

HTH!

HTH !

#3


1  

The portion (true ? null : 0) becomes a function in a way. This function needs a return type. When the compiler needs to figure out the return type it can't.

部分(真的吗?null: 0)在某种程度上成为函数。这个函数需要返回类型。当编译器需要计算返回类型时,它不能。

This works:

如此:

int? i;
i = (true ? null : (int?)0);