Why assigning null in ternary operator fails: no implicit conversion between null and int?
问题 This fails with a There is no implicit conversion between 'null' and 'int' long? myVar = Int64.Parse( myOtherVar) == 0 ? null : Int64.Parse( myOtherVar); However, this succeeds: if( Int64.Parse( myOtherVar) == 0) myVar = null; else myVar = Int64.Parse( myOtherVar); Is there a way to make the ternary operator succeed? 回答1: The compiler ignores the left-hand side when figuring out the type of the right-hand side. So when it tries to deduce the type of Int64.Parse(myOtherVar) == 0 ? null : Int64