The explicit numeric conversions are the conversions from a numeric-type to another numeric-type for which an implicit numeric conversion (§6.1.2) does not already exist:
sbyte
to byte
, ushort
, uint
, ulong
, or char
.byte
to sbyte
and char
.short
to sbyte
, byte
, ushort
, uint
, ulong
, or char
.ushort
to sbyte
, byte
, short
, or char
.int
to sbyte
, byte
, short
, ushort
, uint
, ulong
, or char
.uint
to sbyte
, byte
, short
, ushort
, int
, or char
.long
to sbyte
, byte
, short
, ushort
, int
, uint
, ulong
, or char
.ulong
to sbyte
, byte
, short
, ushort
, int
, uint
, long
, or char
.char
to sbyte
, byte
, or short
.float
to sbyte
, byte
, short
, ushort
, int
, uint
, long
, ulong
, char
, or decimal
.double
to sbyte
, byte
, short
, ushort
, int
, uint
, long
, ulong
, char
, float
, or decimal
.decimal
to sbyte
, byte
, short
, ushort
, int
, uint
, long
, ulong
, char
, float
, or double
.Because the explicit conversions include all implicit and explicit numeric conversions, it is always possible to convert from any numeric-type to any other numeric-type using a cast expression (§7.6.8).
The explicit numeric conversions possibly lose information or possibly cause exceptions to be thrown. An explicit numeric conversion is processed as follows:
checked
context, the conversion succeeds if the source argument is within the range of the destination type, but throws an OverflowException
if the source argument is outside the range of the destination type.unchecked
context, the conversion always succeeds, and simply consists of discarding the most significant bits of the source value.float
, double
, or decimal
to an integral type, the source value is rounded towards zero to the nearest integral value, and this integral value becomes the result of the conversion. If the resulting integral value is outside the range of the destination type, an OverflowException
is thrown.double
to float
, the double
value is rounded to the nearest float
value. If the double
value is too small to represent as a float
, the result becomes positive zero or negative zero. If the double
value is too large to represent as a float
, the result becomes positive infinity or negative infinity. If the double
value is NaN, the result is also NaN.float
or double
to decimal
, the source value is converted to decimal
representation and rounded to the nearest number after the 28th decimal place if required (§4.1.6). If the source value is too small to represent as a decimal
, the result becomes zero. If the source value is NaN, infinity, or too large to represent as a decimal
, an InvalidCastException
is thrown.decimal
to float
or double
, the decimal
value is rounded to the nearest double
or float
value. While this conversion may lose precision, it never causes an exception to be thrown.