[英]Nullable type issue with ?: Conditional Operator
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'."后一种形式给我一个编译错误“无法确定条件表达式的类型,因为‘<null>’和‘System.DateTime’之间没有隐式转换。”
Not that I can't use the former, but the second style is more consistent with the rest of my code.并不是说我不能使用前者,而是第二种风格与我的其余代码更一致。
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?
注意
Nullable<DateTime>
可以写成DateTime?
which will save you a bunch of typing.这将为您节省大量打字时间。
FYI (Offtopic, but nifty and related to nullable types) we have a handy operator just for nullable types called the null coalescing operator仅供参考(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);
这是因为在三元运算符中,两个值必须解析为相同的类型。
Another solution similar to the accepted is to use C#'s default
keyword.另一个类似于已接受的解决方案是使用 C# 的
default
关键字。 While defined using generics, it is actually applicable to any type.虽然使用泛型定义,但它实际上适用于任何类型。
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.此外,通过使用
default
,您无需将变量指定为nullable
为nullable
来为其分配null
值。 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);
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.我知道这个问题是在 2008 年提出的,现在已经 5 年了,但标记为答案的答案并不满足我。 The real answer is that DateTime is a struct, and as a struct it is not compatible with null.
真正的答案是 DateTime 是一个结构体,作为一个结构体,它与 null 不兼容。 You have two ways of solving that:
你有两种解决方法:
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 票的绅士所建议的,或者将 null 转换为 Object 或 ValueType)。
The second is to make the DateTime compatible with null (for instance, cast DateTime to DateTime?).第二个是使 DateTime 与 null 兼容(例如,将 DateTime 转换为 DateTime?)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.