The C# 4.0 compiler does not complain about this (not even a warning):
if(10.0 > null + 1)
{
}
if (myDoubleValue > null)
{
}
And it seems to be always false. What is going on here? Is null automatically converted to Nullable<double> or something?
If so why doesn’t this work then:
double myDoubleValue = null + 1;
Also, why would I ever want such a behavior. Why is it a good thing that it is possible playing around with literals like this.
The reason the assignment doesn’t work is that the result is of type
double?, notdouble. The result will always be the null value for thedouble?(akaNullable<double>) type.Oh, and both of the first two blocks should make the compiler complain, with warnings like this:
Managing to compile without errors isn’t the same as not complaining 🙂