C# ?: 条件运算符的可空类型问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/295833/
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
Nullable type issue with ?: Conditional Operator
提问by Nick Gotch
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.
并不是说我不能使用前者,而是第二种风格与我的其余代码更一致。
采纳答案by Stewart Johnson
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>
可以编写DateTime?
这将节省您大量的打字时间。
回答by MojoFilter
It's because in a ternary operator, the two values must resolve to the same type.
这是因为在三元运算符中,两个值必须解析为相同的类型。
回答by FlySwat
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);
回答by newfurniturey
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# 的default
关键字。虽然使用泛型定义,但它实际上适用于任何类型。
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:
此外,通过使用default
,您无需指定变量nullable
即可为其分配null
值。编译器将自动分配特定变量类型的默认值,不会遇到错误。例子:
DateTime foo;
foo = true ? default(DateTime) : new DateTime(0);
回答by Mishax
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 票的绅士所建议的,或者将 null 转换为 Object 或 ValueType)。
The second is to make the DateTime compatible with null (for instance, cast DateTime to DateTime?).
第二个是使 DateTime 与 null 兼容(例如,将 DateTime 转换为 DateTime?)。