Why assigning null in ternary operator fails: no implicit conversion between null and int?

前端 未结 5 2011
青春惊慌失措
青春惊慌失措 2020-12-11 16:08

This fails with a There is no implicit conversion between \'null\' and \'int\'

long? myVar = Int64.Parse( myOtherVar) == 0 ? null : Int64.Parse(         


        
5条回答
  •  青春惊慌失措
    2020-12-11 16:29

    I am sure you meant to say:

      myVar = value == 0 ? null : (long?)value;
    

    instead of

      myVar = value == 0 ? null : value;
    

    I liked the usage of the 'out' variable. Thanks.

提交回复
热议问题