What is implicit conversion?
Implicit Conversion
Implicit conversion is used directly. For example, you can use a byte type directly on the int type.
For example, the following directly assigns the byte B to the int n, which is directly used. No additional keywords exist, and the system automatically converts all types.
Copy codeThe Code is as follows: byte B = 1;
Int n = B;
Implicit numeric conversion table (from MSDN)
Slave |
To |
Sbyte |
Short, int, long, float, double, or decimal |
Byte |
Short, ushort, int, uint, long, ulong, float, double, or decimal |
Short |
Int, long, float, double, or decimal |
Ushort |
Int, uint, long, ulong, float, double, or decimal |
Int |
Long, float, double, or decimal |
Uint |
Long, ulong, float, double, or decimal |
Long |
Float, double, or decimal |
Char |
Ushort, int, uint, long, ulong, float, double, or decimal |
Float |
Double |
Ulong |
Float, double, or decimal |
Remarks (from MSDN)
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.