What is an explicit conversion
Explicit Conversion
It is when converting one type to another type, additional code is needed to complete this conversion.
int n = 1;
byte b = (byte)n; // Correct, explicit conversion
byte b2 = n; // Error
An explicit conversion needs to be noted that its results are not necessarily what we want.
int n = 256;
byte b = (byte)n; // The result is 0
The result above is 0, because it exceeds 255, it starts at 0;
If n is 257, then b is 1;
If n is 258, then b is 2;
……
From this we have to say that Convert, the Convert class is used to convert types. It has many methods, such as ToInt32, which is to convert it to int. It involves a large span of types, such as object, string, etc., which can convert object, string, etc. into int, while (int) can only convert numeric types to int.
For more related content, seeThe difference between Convert, Parse, TryParse, (int)。
Explicit numerical conversion table (excerpted from MSDN)
from |
arrive |
---|---|
sbyte |
byte、ushort、uint、ulong or char |
byte |
Sbyte orchar |
short |
sbyte、byte、ushort、uint、ulong or char |
ushort |
sbyte、byte、short or char |
int |
sbyte、byte、short、ushort、uint、ulong or char |
uint |
sbyte、byte、short、ushort、int or char |
long |
sbyte、byte、short、ushort、int、uint、ulong or char |
ulong |
sbyte、byte、short、ushort、int、uint、long or char |
char |
sbyte、byte or short |
float |
sbyte、byte、short、ushort、int、uint、long、ulong、char or decimal |
double |
sbyte、byte、short、ushort、int、uint、long、ulong、char、float or decimal |
decimal |
sbyte、byte、short、ushort、int、uint、long、ulong、char、float or double |
Notes (excerpted from MSDN)
Explicit numerical conversions may result in accuracy loss or throw exceptions.
When converting the decimal value to an integer, the value is rounded to the integer value closest to zero. If the result integer value is outside the range of the target type, an OverflowException is raised.
When converting a double or float value to an integer, the value is truncated. If the result integer value is outside the target value, the result will depend on the overflow checking context. In the checked context, an OverflowException is raised; and in the unchecked context, the result will be a value of the target type that is not specified.
When converting a double to float, the double value is rounded to the closest float value. If the double value is too small or too large that the target type cannot accommodate it, the result will be zero or infinity.
When converting float or double to decimal, the source value is converted to a decimal representation and rounded to the closest number after the 28th decimal digit (if needed). Depending on the source value, the following results may be produced:
If the source value is too small to be expressed as decimal, the result will be zero.
An OverflowException is raised if the source value is NaN (non-numeric value), infinity, or cannot be expressed as decimal because it is too large.
When converting decimal to float or double, the decimal value is rounded to the closest double or float value.