Implicit numeric conversions consist of the following:
From sbyte type to 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 the decimal type.
From ushort type to int,uint,long,ulong,float,double, or decimal type.
from type int to long,float,double, or decimal type.
From UINT type to long,ulong,float,double, or decimal type.
From type long to float,double, or decimal type.
From ulong type to 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.
Among them, conversion from int,uint, or long to float, and from long to double may result in a decrease in precision, but will never cause a loss of quantity. Other implicit numeric conversions do not have any loss of information.
In conjunction with the range of value types we learn in data types, we can see that implicit numeric conversions are actually conversions from a low precision numeric type to a high-precision numeric type.
From the above 10 we can see that there is no implicit conversion to the char type, which means that other integer values cannot be automatically converted to the Char type. We need to pay special attention to this point.
The following procedure gives an example of an implicit numeric conversion.
Program Listing 6-1:
Using System;
Class Test
{public
static void Main ()
{
byte x=16;
Console.WriteLine ("X={0}", x);
UShort Y=x;
Console.WriteLine ("Y={0}", y);
y=65535;
Console.WriteLine ("Y={0}", y);
float z=y;
Console.WriteLine ("Z={0}", z);
}
The output of the program will be:
x=16;
y=16;
y=65535;
z=65535;
If we add a sentence after the statement in the above program:
y=y+1;
When you recompile the program, the compiler will give an error message:
Can not implictly convert type ' int ' to type ' ushort '
This shows that there is no implicit conversion from the integer type 65536 to the unsigned short integer y.