显式数值转换表(C# 参考)Explicit numeric conversions table (C# Reference)

下表显示没有隐式转换的 .NET 数值类型之间的预定义显式转换。The following table shows the predefined explicit conversions between .NET numeric types for which there is no implicit conversion.

FromFrom To
sbytesbyte byteushortuintulongcharbyte, ushort, uint, ulong, or char
bytebyte sbytecharsbyte or char
shortshort sbytebyteushortuintulongcharsbyte, byte, ushort, uint, ulong, or char
ushortushort sbytebyteshortcharsbyte, byte, short, or char
intint sbytebyteshortushortuintulongcharsbyte, byte, short, ushort, uint, ulong,or char
uintuint sbytebyteshortushortintcharsbyte, byte, short, ushort, int, or char
longlong sbytebyteshortushortintuintulongcharsbyte, byte, short, ushort, int, uint, ulong, or char
ulongulong sbytebyteshortushortintuintlongcharsbyte, byte, short, ushort, int, uint, long, or char
charchar sbytebyteshortsbyte, byte, or short
floatfloat sbytebyteshortushortintuintlongulongchardecimalsbyte, byte, short, ushort, int, uint, long, ulong, char,or decimal
doubledouble sbytebyteshortushortintuintlongulongcharfloatdecimalsbyte, byte, short, ushort, int, uint, long, ulong, char, float,or decimal
decimaldecimal sbytebyteshortushortintuintlongulongcharfloatdoublesbyte, byte, short, ushort, int, uint, long, ulong, char, float, or double

备注Remarks

  • 显式数值转换可能会导致精度降低或导致引发异常,通常为 OverflowExceptionThe explicit numeric conversion may cause loss of precision or result in throwing an exception, typically an OverflowException.

  • 将整数类型的值转换为另一个整数类型时,结果取决于溢出检查上下文When you convert a value of an integral type to another integral type, the result depends on the overflow checking context. 在已检查的上下文中,如果源值在目标类型的范围内,则转换成功。In a checked context, the conversion succeeds if the source value is within the range of the destination type. 否则会引发 OverflowExceptionOtherwise, an OverflowException is thrown. 在未检查的上下文中,转换始终成功,并按如下方式进行:In an unchecked context, the conversion always succeeds, and proceeds as follows:

    • 如果源类型大于目标类型,则通过放弃其“额外”最高有效位来截断源值。If the source type is larger than the destination type, then the source value is truncated by discarding its "extra" most significant bits. 结果会被视为目标类型的值。The result is then treated as a value of the destination type.

    • 如果源类型小于目标类型,则源值是符号扩展或零扩展,以使其与目标类型的大小相同。If the source type is smaller than the destination type, then the source value is either sign-extended or zero-extended so that it is the same size as the destination type. 如果源类型带符号,则是符号扩展;如果源类型是无符号的,则是零扩展。Sign-extension is used if the source type is signed; zero-extension is used if the source type is unsigned. 结果会被视为目标类型的值。The result is then treated as a value of the destination type.

    • 如果源类型与目标类型的大小相同,则源值将被视为目标类型的值。If the source type is the same size as the destination type, then the source value is treated as a value of the destination type.

  • decimal 值转换为整型类型时,此值会向零舍入到最接近的整数值。When you convert a decimal value to an integral type, this value is rounded towards zero to the nearest integral value. 如果生成的整数值处于目标类型的范围之外,则会引发 OverflowExceptionIf the resulting integral value is outside the range of the destination type, an OverflowException is thrown.

  • doublefloat 值转换为整型类型时,此值会向零舍入到最接近的整数值。When you convert a double or float value to an integral type, this value is rounded towards zero to the nearest integral value. 如果生成的整数值处于目标类型范围之外,则结果会取决于溢出上下文If the resulting integral value is outside the range of the destination type, the result depends on the overflow checking context. 在已检查的上下文中,引发 OverflowException;而在未检查的上下文中,结果是目标类型的未指定值。In a checked context, an OverflowException is thrown, while in an unchecked context, the result is an unspecified value of the destination type.

  • double 转换为 float 时,double 值舍入为最接近的 float 值。When you convert double to float, the double value is rounded to the nearest float value. 如果 double 值太小或太大,无法匹配目标类型,结果将为零或无穷大。If the double value is too small or too large to fit into the destination type, the result will be zero or infinity.

  • floatdouble 转换为 decimal 时,源值转换为 decimal 表示形式,并并五入到第 28 位小数后最接近的数(如果需要)。When you convert float or double to decimal, the source value is converted to decimal representation and rounded to the nearest number after the 28th decimal place if required. 根据源值的值,可能出现以下结果之一:Depending on the value of the source value, one of the following results may occur:

    • 如果源值太小,无法表示为 decimal,结果则为零。If the source value is too small to be represented as a decimal, the result becomes zero.

    • 如果源值为 NaN(非数值)、无穷大或太大而无法表示为 decimal,则引发 OverflowExceptionIf the source value is NaN (not a number), infinity, or too large to be represented as a decimal, an OverflowException is thrown.

  • decimal 转换为 floatdouble 时,decimal 值舍入到最接近的 doublefloat 值。When you convert decimal to float or double, the decimal value is rounded to the nearest double or float value.

有关显式转换的详细信息,请参阅 C# 语言规范显式转换章节。For more information about explicit conversions, see the Explicit conversions section of the C# language specification.

请参阅See also