【发布时间】:2010-09-22 16:32:36
【问题描述】:
有人可以解释为什么这在 C#.NET 2.0 中有效:
Nullable<DateTime> foo;
if (true)
foo = null;
else
foo = new DateTime(0);
...但这不是:
Nullable<DateTime> foo;
foo = true ? null : new DateTime(0);
后一种形式给我一个编译错误“条件表达式的类型无法确定,因为'
不是我不能使用前者,而是第二种风格更符合我的其余代码。
【问题讨论】:
-
您可以通过使用 DateTime 为自己节省大量打字时间吗?而不是 Nullable
.
标签: c# generics nullable conditional-operator