Possible Duplicate:
No implicit int -> short conversion in ternary statement
So I understand the fundamentals of type conversion and inference of types in the C# ternary operator, thanks mostly to Eric Lippert's answer to this question. But the thing I'm still not understanding is what I have below.
I guess I understand that C# is assuming the zero is an int
because that's somehow the default, so an error is thrown when I try to assign an int
to a short
variable:
short recordNumber = Convert.IsDBNull(dr["record_number"]) ? 0
: Convert.ToInt16(dr["record_number"]);
This error gets thrown:
Cannot implicitly convert type 'int' to 'short'.
An explicit conversion exists (are you missing a cast?)
This is what VS10 is looking for:
short recordNumber = Convert.IsDBNull(dr["record_number"]) ? (short)0
: Convert.ToInt16(dr["record_number"]);
What I'm trying to understand is why is 0 chosen to be int
rather than short
? If it was chosen to be a short
then that would make it more universally "assignable". Is there some reason for this that I'm missing?