Why does null need to be casted?
- by BlueRaja The Green Unicorn
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 does null need to be casted, and why can't we simply cast the whole expression (which I know is legal in other cases)?