Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does null need an explicit type cast here? [duplicate]

Tags:

The following code does not compile:

//int a = ... int? b = (int?) (a != 0 ? a : null); 

In order to compile, it needs to be changed to

int? b = (a != 0 ? a : (int?) null); 

Since both b = null and b = a are legal, this doesn't make sense to me.

Why do we have to cast the null into an int? and why can't we simply provide an explicit type cast for the whole expression (which I know is possible in other cases)?

like image 763
BlueRaja - Danny Pflughoeft Avatar asked Apr 09 '10 15:04

BlueRaja - Danny Pflughoeft


2 Answers

From chapter 7.13 of the C# Language Specification:

The second and third operands of the ?: operator control the type of the conditional expression. Let X and Y be the types of the second and third operands. Then,

  • If X and Y are the same type, then this is the type of the conditional expression.
  • Otherwise, 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.
  • Otherwise, 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.

In your case, there is no implicit conversion from int to null nor the other way around. Your cast solves the problem, int is convertible to int?

like image 98
Hans Passant Avatar answered Jan 24 '23 12:01

Hans Passant


The both alternatives of ?: operator must be of the same type. Otherwise, the compiler cannot deduce the type of the whole conditional expression.

null is not an int, so you need to give a hint to the compiler that the resulting type is int?.


Edit: as the others pointed out, the two types don't need to be the same, but one of them should be castable to another (that another one will be the result type). See specs for more details.

like image 39
Vlad Avatar answered Jan 24 '23 11:01

Vlad