double? test = true ? null : 1.0;
In my book, this is the same as
if (true) {
test = null;
} else {
test = 1.0;
}
But the first line gives this compiler error:
Type of conditional expression cannot
be determined because there is no
implicit conversion between ‘<null>‘
and ‘double‘.
This happens because the compiler tries to evaluate the statement from right to left.
This means that it sees
1.0and it decides it is double (not double?) and then it seesnull.So there is clearly no implicit conversion between
doubleandnull(in fact there is no implicit conversion betweenStructandnull).What you can do is explicitly tell the compiler that one of the two expressions that are convertible to each other.