Implicit conversion and display conversion are available in every language, and C # is no exception. Generally, when writing an arithmetic expression, you must ensure that the overall data type is consistent and the calculation is accurate. For example, if a variable a is of the int type, however, it is obtained through addition, subtraction, multiplication, division, and display conversion are considered in the operation. The division operation needs to be converted to float or double, and then the result is forcibly converted to int; the other is when passing parameters, you need to pay attention to the need to display the conversion, or when assigning values to the set, you also need to pay attention to the type issues.
1. implicit type conversion
Implicit conversion refers to the default system conversion. In essence, the data type of small storage capacity is automatically converted to the Data Type of large storage capacity. There are several types:
From the sbyte type to the short, int, long, float, double, or decimal type.
From byte type to short, ushort, int, uint, long, ulong, float, double, or decimal type.
From the short type to the int, long, float, double, or decimal type.
From ushort type to int, uint, long, ulong, float, double, or decimal type.
From int type to long, float, double, or decimal type.
From the uint type to the long, ulong, float, double, or decimal type.
From the long type to the float, double, or decimal type.
From the ulong type to the float, double, or decimal type.
From char type to ushort, int, uint, long, ulong, float, double, or decimal type.
From float type to double type.
2. Explicit type conversion
Explicit type conversion, that is, forced type conversion. In contrast to implicit conversions, forced conversions can cause data loss.
From sbyte to byte, ushort, uint, ulong, or char.
From byte to sbyte or char.
From short to sbyte, byte, ushort, uint, ulong, or char.
From ushort to sbyte, byte, short, or char.
From int to sbyte, byte, short, ushort, uint, ulong, or char.
From uint to sbyte, byte, short, ushort, int, or char.
From long to sbyte, byte, short, ushort, int, uint, ulong, or char.
From ulong to sbyte, byte, short, ushort, int, uint, long, or char.
From char to sbyte, byte, or short.
From float to sbyte, byte, short, ushort, int, uint, long, ulong, char, or decimal.
From double to sbyte, byte, short, ushort, int, uint, long, ulong, char, float, or decimal.
From decimal to sbyte, byte, short, ushort, int, uint, long, ulong, char, float, or double.
If the conversion has lost information or causes an exception to be thrown, the conversion is performed according to the following rules:
For the conversion from one integer to another, the compiler will detect the overflow of the conversion. If no overflow occurs, the conversion is successful. Otherwise, an OverflowException exception is thrown. This check is also related to whether the checked option is set in the compiler.
For the conversion from float, double, or decimal to integer, the value of the source variable is rounded to the nearest integer value as the conversion result. If the integer value exceeds the value range of the target type, an OverflowException exception is thrown.
For the conversion from double to float, the double value is rounded to the nearest float value. If the value is too small, the result will be positive 0 or negative 0; if the value is too large, it will become positive
Infinite or negative infinity. If the original double value is Nan, the conversion result is NaN.
For conversions from float or double to decimal, the source value is converted to the decimal form and rounded to the 28 digits after the decimal point (if necessary ). If the source value is too small, the result is 0. If it is too large to be expressed in decimal places, or infinite or NaN, an InvalidCastException is thrown.
For the conversion from decimal to float or double, the decimal value is rounded to the nearest value. This conversion may cause loss of precision, but will not cause exceptions.
From yysyangyangyangshan