Make conditional expression 'condition ? 1 : null' compile and be int?

Topics: C# Language Design
Jul 24, 2014 at 5:13 PM
Just a simple one, according to the C# Spec this is the type of the Conditional operator.
  • If x has type X and y has type Y then
    • If an implicit conversion (§6.1) exists from X to Y, but not from Y to X, then Y is the type of the conditional expression.
    • If an implicit conversion (§6.1) exists from Y to X, but not from X to Y, then X is the type of the conditional expression.
  • Otherwise, no expression type can be determined, and a compile-time error occurs.
    • If only one of x and y has a type, and both x and y, of are implicitly convertible to that type, then that is the type of the conditional expression.
    • Otherwise, no expression type can be determined, and a compile-time error occurs.
The type of 1 is int and null has no type, so true ? 1 : null does not compile. I like that C# doesn't invent types but I think an exception should be made for Nullable<T>
Jul 25, 2014 at 1:30 PM
That's a very nice idea. Support++