C#中什么情况下使用隐式数据转换和显式转换

来源:百度知道 编辑:UC知道 时间:2024/09/22 17:20:23

养成习惯通通用显示转换比较好,虽然代码写得多一点,但是看起来明白

下面是可以隐式转换的类型。说白了,一句话,从小的向大的转一般都可以隐式转,倒过来就不一定行了

C# 语言参考
隐式数值转换表(C# 参考)

下表显示了预定义的隐式数值转换。隐式转换可能在多种情形下发生,包括调用方法时和在赋值语句中。

从 到
sbyte
short、int、long、float、double 或 decimal

byte
short、ushort、int、uint、long、ulong、float、double 或 decimal

short
int、long、float、double 或 decimal

ushort
int、uint、long、ulong、float、double 或 decimal

int
long、float、double 或 decimal

uint
long、ulong、float、double 或 decimal

long
float、double 或 decimal

char
ushort、int、 uint、 long、ulong、 float、double 或 decimal

float
double

ulong
float、 double 或 decimal