I don't know if this is a compiler error or not. It all kind of makes sense
but you would think that the compiler would check the types of the possible
outcomes of the tertiary operator (c and d) against the type of the variable
being assigned (a). Instead it compares the types of c and d which may not
be compatible. Just seems silly.
int? i;
object o;
//this is ok:
if (i.HasValue)
o = i;
else
o = DBNull.Value;
//this fails to compile
//Error 1 Type of conditional expression cannot be determined because
//there is no implicit conversion between 'int?' and 'System.DBNull'
o = i.HasValue ? i : DBNull.Value;
//these also fail for the same reason
o = i == null ? DBNull.Value: i;
o = i.HasValue ? i : "ABC";
//but these works
o = i.HasValue ? (object)i : DBNull.Value;
o = i.HasValue ? (object)i : (object)DBNull.Value;
o = i.HasValue ? i : (object)DBNull.Value;