Implicit Conversion
The available implicit built-in type conversions in C# are shown in the table below.
|
Type (Conversion From)
|
Legal Implicit Conversion 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
|
The following implicit conversions are never legal:
There are no allowable implicit conversions from bool, double, or decimal types;
There are no implicit conversions allowed to the char type;
There are no implicit conversions allowed between the floating-point types and the decimal type.
Implicit conversions are also possible for more complex reference types. Generally, when you eyeball two reference
types to see whether you can do an implicit conversion, you should be asking the question as with simple type
conversions: Can I guarantee the success of the operation without data loss?
In addition, some rules do apply to reference-type implicit conversions. For example, any reference type can
be implicitly converted to object. And, any derived class can be implicitly converted to the class it was derived from.
It’s time to turn to explicit conversion .
|
|
Search Engine Optimization
 
Syndication Viewer
Our Web host:
IX WebHosting
|