可为Nullable类型问题?:条件运算符

时间:2021-06-26 22:25:25

Could someone explain why this works in C#.NET 2.0:

有人可以解释为什么这在C#.NET 2.0中有效:

    Nullable<DateTime> foo;
    if (true)
        foo = null;
    else
        foo = new DateTime(0);

...but this doesn't:

......但这不是:

    Nullable<DateTime> foo;
    foo = true ? null : new DateTime(0);

The latter form gives me an compile error "Type of conditional expression cannot be determined because there is no implicit conversion between '<null>' and 'System.DateTime'."

后一种形式给我一个编译错误“无法确定条件表达式的类型,因为' '和'System.DateTime'之间没有隐式转换。”

Not that I can't use the former, but the second style is more consistent with the rest of my code.

并不是说我不能使用前者,但第二种风格与我的其余代码更加一致。

5 个解决方案

#1


308  

This question has been asked a bunch of times already. The compiler is telling you that it doesn't know how convert null into a DateTime.

这个问题已被问过很多次了。编译器告诉你它不知道如何将null转换为DateTime。

The solution is simple:

解决方案很简单:

DateTime? foo;
foo = true ? (DateTime?)null : new DateTime(0);

Note that Nullable<DateTime> can be written DateTime? which will save you a bunch of typing.

注意Nullable 可以写成DateTime吗?这将节省你一堆打字。

#2


18  

FYI (Offtopic, but nifty and related to nullable types) we have a handy operator just for nullable types called the null coalescing operator

FYI(Offtopic,但很漂亮并且与可空类型相关)我们有一个方便的运算符,仅用于可空类型,称为空合并运算符

??

Used like this:

像这样使用:

// Left hand is the nullable type, righthand is default if the type is null.
Nullable<DateTime> foo;
DateTime value = foo ?? new DateTime(0);

#3


8  

It's because in a ternary operator, the two values must resolve to the same type.

这是因为在三元运算符中,这两个值必须解析为相同的类型。

#4


4  

Another solution similar to the accepted is to use C#'s default keyword. While defined using generics, it is actually applicable to any type.

另一种类似于接受的解决方案是使用C#的默认关键字。虽然使用泛型定义,但它实际上适用于任何类型。

Example usage applied to the OP's question:

应用于OP问题的示例用法:

Nullable<DateTime> foo;
foo = true ? default(DateTime) : new DateTime(0);

Example usage with the current accepted answer:

使用当前接受的答案的示例用法:

DateTime? foo;
foo = true ? default(DateTime) : new DateTime(0);

Also, by using default, you do not need to specify the variable as nullable in order to assign it a null value. The compiler will auto-assign the specific variable-type's default value and no error will be encountered. Example:

此外,通过使用默认值,您无需将变量指定为可空,以便为其分配空值。编译器将自动分配特定变量类型的默认值,不会遇到任何错误。例:

DateTime foo;
foo = true ? default(DateTime) : new DateTime(0);

#5


3  

I know this question was asked in 2008 and it is now 5 years later but the answer marked as an answer does not satisfy me. The real answer is that DateTime is a struct, and as a struct it is not compatible with null. You have two ways of solving that:

我知道这个问题是在2008年提出的,现在已经过了5年,但答案标记为答案并不能让我满意。真正的答案是DateTime是一个结构,并且作为结构,它与null不兼容。你有两种解决方法:

First is to make null compatible with DateTime (for instance, cast null to DateTime? as the gentleman with 70 upvotes suggests, or cast null to Object or to ValueType).

首先是使null与DateTime兼容(例如,将null转换为DateTime?作为具有70个upvotes的绅士建议,或者将null转换为Object或ValueType)。

The second is to make the DateTime compatible with null (for instance, cast DateTime to DateTime?).

第二个是使DateTime与null兼容(例如,将DateTime转换为DateTime?)。

#1


308  

This question has been asked a bunch of times already. The compiler is telling you that it doesn't know how convert null into a DateTime.

这个问题已被问过很多次了。编译器告诉你它不知道如何将null转换为DateTime。

The solution is simple:

解决方案很简单:

DateTime? foo;
foo = true ? (DateTime?)null : new DateTime(0);

Note that Nullable<DateTime> can be written DateTime? which will save you a bunch of typing.

注意Nullable 可以写成DateTime吗?这将节省你一堆打字。

#2


18  

FYI (Offtopic, but nifty and related to nullable types) we have a handy operator just for nullable types called the null coalescing operator

FYI(Offtopic,但很漂亮并且与可空类型相关)我们有一个方便的运算符,仅用于可空类型,称为空合并运算符

??

Used like this:

像这样使用:

// Left hand is the nullable type, righthand is default if the type is null.
Nullable<DateTime> foo;
DateTime value = foo ?? new DateTime(0);

#3


8  

It's because in a ternary operator, the two values must resolve to the same type.

这是因为在三元运算符中,这两个值必须解析为相同的类型。

#4


4  

Another solution similar to the accepted is to use C#'s default keyword. While defined using generics, it is actually applicable to any type.

另一种类似于接受的解决方案是使用C#的默认关键字。虽然使用泛型定义,但它实际上适用于任何类型。

Example usage applied to the OP's question:

应用于OP问题的示例用法:

Nullable<DateTime> foo;
foo = true ? default(DateTime) : new DateTime(0);

Example usage with the current accepted answer:

使用当前接受的答案的示例用法:

DateTime? foo;
foo = true ? default(DateTime) : new DateTime(0);

Also, by using default, you do not need to specify the variable as nullable in order to assign it a null value. The compiler will auto-assign the specific variable-type's default value and no error will be encountered. Example:

此外,通过使用默认值,您无需将变量指定为可空,以便为其分配空值。编译器将自动分配特定变量类型的默认值,不会遇到任何错误。例:

DateTime foo;
foo = true ? default(DateTime) : new DateTime(0);

#5


3  

I know this question was asked in 2008 and it is now 5 years later but the answer marked as an answer does not satisfy me. The real answer is that DateTime is a struct, and as a struct it is not compatible with null. You have two ways of solving that:

我知道这个问题是在2008年提出的,现在已经过了5年,但答案标记为答案并不能让我满意。真正的答案是DateTime是一个结构,并且作为结构,它与null不兼容。你有两种解决方法:

First is to make null compatible with DateTime (for instance, cast null to DateTime? as the gentleman with 70 upvotes suggests, or cast null to Object or to ValueType).

首先是使null与DateTime兼容(例如,将null转换为DateTime?作为具有70个upvotes的绅士建议,或者将null转换为Object或ValueType)。

The second is to make the DateTime compatible with null (for instance, cast DateTime to DateTime?).

第二个是使DateTime与null兼容(例如,将DateTime转换为DateTime?)。