Nullable types and the ternary operator: why is `? 10 : null` forbidden? [duplicate]

I just came across a weird error:

private bool GetBoolValue()
{
    //Do some logic and return true or false
}

Then, in another method, something like this:

int? x = GetBoolValue() ? 10 : null;

Simple, if the method returns true, assign 10 to the Nullableint x. Otherwise, assign null to the nullable int. However, the compiler complains:

Error 1 Type of conditional expression cannot be determined because there is no implicit conversion between int and <null>.

Am I going nuts?

9 Answers
9

Leave a Comment