隐式数值转换表(C# 参考)
下表显示预定义隐式数值转换。 隐式转换可能会在许多情况下出现(包括方法调用和赋值语句)。
From | 到 |
---|---|
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
|
备注
-
在从
int
、uint
、long
或ulong
转换为float
,以及从long
或ulong
转换为double
时,可能会丢失精度,但不会丢失量值。 -
不存在针对
char
类型的隐式转换。 -
浮点类型与
decimal
类型之间不存在隐式转换。 -
int
类型的常数表达式可以转换为sbyte
、byte
、short
、ushort
、uint
或ulong
,前提是常数表达式的值处于目标类型的范围内。
C# 语言规范
有关详细信息,请参阅 C# 语言规范。 该语言规范是 C# 语法和用法的权威资料。