Long story short, yes, it is safe to assume that the compiler will assume double
when at least one side of a binary operation is typed as that. You do not need to be explicit (although of course there are times when it might be useful from a readability standpoint, which isn't really relevant but it bears mention).
And just to be clear, although I don't think you've got this confused in your original question, but the type of variable to which you assign the result has no effect on how the division operator overload is determined.
Your top case simply utilizes an implicit conversion, which would have the explicit equivalent of this.
double a = (double)(1 / 1234);
The fact that you assign it to a double
does not effect the fact that 1 / 1234
evaluates, as you've found, to 0
, because that cast occurs post-facto to the division. If we cut a hole in space-time here, this example would fall to the very last case of the list at the bottom of this post, where two integers are divided. Thus, as per section 7.8.2 of the spec, this will return the floor of the quotient, or 0
.
This behavior is described in section 7.3.6 of the C# 5 spec (it took a bit of looking, I'll admit), with emphasis mine.
7.3.6 Numeric promotions
...
When overload resolution rules (§7.5.3) are applied to this set of operators, the effect is to select the first of the operators for which implicit conversions exist from the operand types. For example, for the operation b * s, where b is a byte and s is a short, overload resolution selects operator *(int, int) as the best operator. Thus, the effect is that b and s are converted to int, and the type of the result is int. Likewise, for the operation i * d, where i is an int and d is a double, overload resolution selects operator *(double, double) as the best operator.
This, of course, applies not only to multiplication as its examples show, but any binary operation.
Just for good measure, the hierarchy is as follows, taken from 7.3.6.2, emphasis still mine.
• If either operand is of type decimal, the other operand is converted to type decimal, or a binding-time error occurs if the other operand is of type float or double.
• Otherwise, if either operand is of type double, the other operand is converted to type double.
• Otherwise, if either operand is of type float, the other operand is converted to type float.
• Otherwise, if either operand is of type ulong, the other operand is converted to type ulong, or a binding-time error occurs if the other operand is of type sbyte, short, int, or long.
• Otherwise, if either operand is of type long, the other operand is converted to type long.
• Otherwise, if either operand is of type uint and the other operand is of type sbyte, short, or int, both operands are converted to type long.
• Otherwise, if either operand is of type uint, the other operand is converted to type uint.
• Otherwise, both operands are converted to type int.
In reality, the compiler actually performs that cast for you, by making the non-double
operand into type double
.