?: Operator (C # link)
Any type of expression first_expression and second_expression must be the same, or an implicit conversion must exist from one type to another.
Integer literals
If a literal does not have a suffix, it has the first of these types in which its value can be represented: int, uint, long, ulong.
Consider:
var value = test ? (Int64)1 : 0;
0, decimal digital literal without suffix will be converted to int . int can be implicitly converted to Int64 . Since this conversion takes place in only one direction, we can feel safe that the return value will be Int64.
But:
var value = test ? (UInt64)1 : 0;
UInt64 and int cannot be implicitly converted to each other, but this code is compiled and run, and the resulting type is UInt64 .
At what point is type 0 determined?
If two types are implicitly hidden with each other, then which of the two types will you find yourself in? (I do not think this is going well, but classes created by the user can implement such casting.)
Previous research:
I found several other questions with similar headings, but they were all related to null or null types.
Relevance: It matters in my code, because we immediately pass this result to ByteWriter.Write and want to get the correct overload, which writes the correct number of bytes. The examples, of course, are greatly simplified.
Alternative syntax makes the result explicit may be the best option for clarity, regardless of what actually happens without an explicit cast:
var value = test ? (UInt64)1 : (UInt64)0;