The compiler tries to evaluate the right-hand expression. null
is null
and the 0
is an int
literal, not int?
. The compiler is trying to tell you that it can’t determine what type the expression should evaluate as. There’s no implicit conversion between null
and int
, hence the error message.
You need to tell the compiler that the expression should evaluate as an int?
. There is an implicit conversion between int?
and int
, or between null
and int?
, so either of these should work:
int? x = true ? (int?)null : 0;
int? y = true ? null : (int?)0;