I sort of ran into this today when writing some code. Take the following as an example:
long valueCast = (long)(10 + intVariable);
long valueTyped = 10L + intVariable;
Is there any difference between these two or are they compiled to exactly the same thing? Is there a convention for one over the other?
So I know this isn't a critical question (both work). I'm just very curious about what the difference(s) might be!
EDIT - Modified the code example to be closer to what my original scenario actually is. I wanted the question to be clear so I replaced the variable with a constant. Didn't realize the compiler would to the arithmetic automatically (thereby changing the answers to this question)
Yes, there's a big difference between those two. Not for those particular values, but the exact same expression with different values will show the difference.
In the first version, the addition is done in 32-bit arithmetic, and then the result is converted into a long
.
In the second version, the first operand is already a long
, so the second operand is promoted to long
and the addition is then performed in 64-bit arithmetic.
Of course in this case the compiler will perform the arithmetic itself anyway (and come to the same conclusion in both cases), but it's important to understand the difference between converting the result of an operation, and converting one of the operands of an operation.
As an example:
unchecked
{
long valueCast = (long)(2000000000 + 2000000000);
long valueTyped = 2000000000L + 2000000000;
Console.WriteLine(valueCast);
Console.WriteLine(valueTyped);
}
Result:
-294967296
4000000000
Note that this has to be done in an explicitly unchecked context, as otherwise the first addition wouldn't even compile - you'd get an error of "CS0220: The operation overflows at compile time in checked mode".
See more on this question at Stackoverflow