相关疑难解决方法(0)

为什么在三元运算符中赋值null失败:null和int之间没有隐式转换?

这失败了 There is no implicit conversion between 'null' and 'int'

long? myVar = Int64.Parse( myOtherVar) == 0 ? null : Int64.Parse( myOtherVar);
Run Code Online (Sandbox Code Playgroud)

但是,这成功了:

if( Int64.Parse( myOtherVar) == 0)
    myVar = null;
else
    myVar = Int64.Parse( myOtherVar);
Run Code Online (Sandbox Code Playgroud)

有没有办法让三元运算符成功?

c# ternary-operator

11
推荐指数
1
解决办法
6264
查看次数

标签 统计

c# ×1

ternary-operator ×1