As mentioned above, implicit conversion does not require any syntax statement, for example:
Byte bt = 1;
Short s = 35;
Int x = bt; // The byte type is directly transferred to the int type, which is an implicit conversion.
Int y = s; // same as above
There is a premise for conversion, that is, as long as the value does not change, the type conversion can be successful. However, before conversion, the compiler checks whether the value is guaranteed or not. The compiler only looks at the size of the type. Therefore, the premise of implicit conversion is that the type with a small number of digits is converted to a large one, and the value can be automatically converted without changing.
As you can see from the above two instances, both are extended conversions. For example, in the following example, if you want to convert the int type to the byte type, an error will occur if you want to implement it implicitly, because the int type is larger than the byte type, this may cause data loss. Therefore, the compiler will prompt an error. For example, we declare an int type variable first, and then try to implicitly convert the int type variable to the byte type. 2-20
Figure 2-20
The following table shows the predefined implicit numeric conversion. Implicit conversions may occur in many situations, including calling methods and in the value assignment statement.
Note the following points for implicit conversion:
The non-empty type conversion rules in the table must be followed. Int? Implicitly converted to long? , Float? , Double? And decimal ?.
Implicit conversion of non-empty types to empty types also follows the conversion rules in the table above, that is, int implicit conversion to long? , Float? , Double? And decimal ?.
An empty type cannot be implicitly converted to a non-empty type. explicit conversion is required, as described in the following section. This is because the value of the null type can be null, but the non-null type cannot represent this value.
The conversion from int, uint, long to float and the conversion from long to double may be less precise, but the value size is not affected.
There is no implicit conversion to the char type.
There is no implicit conversion between the floating point type and the decimal type.
A constant expression of the int type can be converted to sbyte, byte, short, ushort, uint, or ulong, provided that the value of the constant expression is within the range of the target type.
In addition, implicit conversions are not suitable for the following scenarios:
● Converting int to short -- data loss
● Converting int to uint -- data loss
● Convert uint to int -- data will be lost
● Float to int -- all data after the decimal point will be lost
● Convert any numeric type to char -- data will be lost
● Convert decimal to any numeric type -- because the internal structure of decimal is different from integer and floating point
● Int? Convert to int -- the value of the null type can be null.
If the conversion is required, display conversion must be used to force the compiler to perform the conversion.
This article is a new one. For more information, see the source and author!