If numeric expression contains operands (constants and variables) of different numeric types, are operands promoted to larger types according to the following rules:
byte
, sbyte
, char
, short
, ushort
, they get converted to int
typeint
, then all operands are converted to int
uint
and int
, then all operands are converted to long
long
, then all operands are converted to long
ulong
and long
, then operands are converted to float
float
, then all operands are converted to float
double
, then all operands are converted to double
Assuming numeric expressions contains operands of different types, will all operands first get converted to a single numeric type, and only then will the runtime try to compute the result? For example, if variables b1
and b2
are of byte
type, while i1
is of int
type, will b1
and b2 get
converted to int prior to computing (b1+b2)
:
int i2=(b1+b2)+i1
The parentheses are of higher precedence than +
, so the conversion would normally take place after b1
and b2
have been added. However, the +
operator does not have an overload for byte
s, so the byte
s must first be promoted to int
s.
Further reading:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With