相关疑难解决方法(0)

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

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

    Nullable<DateTime> foo;
    if (true)
        foo = null;
    else
        foo = new DateTime(0);
Run Code Online (Sandbox Code Playgroud)

......但这不是:

    Nullable<DateTime> foo;
    foo = true ? null : new DateTime(0);
Run Code Online (Sandbox Code Playgroud)

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

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

c# generics nullable conditional-operator

154
推荐指数
3
解决办法
6万
查看次数

标签 统计

c# ×1

conditional-operator ×1

generics ×1

nullable ×1