.NET 中的类型转换表Type Conversion Tables in .NET

当一种类型的值转换为大小相等或更大的另一类型时,将发生扩大转换。Widening conversion occurs when a value of one type is converted to another type that is of equal or greater size. 当一种类型的值转换为较小的另一种类型时,将发生收缩转换。A narrowing conversion occurs when a value of one type is converted to a value of another type that is of a smaller size. 本主题中的表格解释了这两种转换类型的行为。The tables in this topic illustrate the behaviors exhibited by both types of conversions.

扩大转换Widening Conversions

下表列出了执行不会导致信息丢失的扩大转换。The following table describes the widening conversions that can be performed without the loss of information.

类型Type 可在不丢失数据的情况下转换为Can be converted without data loss to
Byte UInt16, Int16, UInt32, Int32, UInt64, Int64, Single, Double, DecimalUInt16, Int16, UInt32, Int32, UInt64, Int64, Single, Double, Decimal
SByte Int16, Int32, Int64, Single, Double, DecimalInt16, Int32, Int64, Single, Double, Decimal
Int16 Int32, Int64, Single, Double, DecimalInt32, Int64, Single, Double, Decimal
UInt16 UInt32, Int32, UInt64, Int64, Single, Double, DecimalUInt32, Int32, UInt64, Int64, Single, Double, Decimal
Char UInt16, UInt32, Int32, UInt64, Int64, Single, Double, DecimalUInt16, UInt32, Int32, UInt64, Int64, Single, Double, Decimal
Int32 Int64, Double, DecimalInt64, Double, Decimal
UInt32 Int64, UInt64, Double, DecimalInt64, UInt64, Double, Decimal
Int64 Decimal
UInt64 Decimal
Single Double

一些目标为 SingleDouble 的扩大转换可能会导致精度丢失。Some widening conversions to Single or Double can cause a loss of precision. 下面的表格描述了有时会导致信息丢失的扩大转换。The following table describes the widening conversions that sometimes result in a loss of information.

类型Type 可转换为Can be converted to
Int32 Single
UInt32 Single
Int64 SingleDoubleSingle, Double
UInt64 Single, DoubleSingle, Double
Decimal SingleDoubleSingle, Double

收缩转换Narrowing Conversions

目标为 SingleDouble 的收缩转换可能会导致信息丢失。A narrowing conversion to Single or Double can cause a loss of information. 如果目标类型无法正确表达源类型的大小,则结果类型将设置为常数 PositiveInfinityNegativeInfinityIf the target type cannot properly express the magnitude of the source, the resulting type is set to the constant PositiveInfinity or NegativeInfinity. PositiveInfinity 是正数除以 0 的结果,也在 SingleDouble 的值大于 MaxValue 字段的值时返回。PositiveInfinity results from dividing a positive number by zero and is also returned when the value of a Single or Double exceeds the value of the MaxValue field. NegativeInfinity 是负数除以 0 的结果,也在 SingleDouble 的值小于 MinValue 字段的值时返回。NegativeInfinity results from dividing a negative number by zero and is also returned when the value of a Single or Double falls below the value of the MinValue field. Double 转换到 Single 可能会导致 PositiveInfinityNegativeInfinityA conversion from a Double to a Single might result in PositiveInfinity or NegativeInfinity.

收缩转换还可能导致其他数据类型的信息丢失。A narrowing conversion can also result in a loss of information for other data types. 不过,如果要转换的类型值不在目标类型的 MaxValueMinValue 字段指定的范围内,就会抛出 OverflowException,并且运行时会检查转换,以确保目标类型的值不超出它的 MaxValueMinValueHowever, an OverflowException is thrown if the value of a type that is being converted falls outside of the range specified by the target type's MaxValue and MinValue fields, and the conversion is checked by the runtime to ensure that the value of the target type does not exceed its MaxValue or MinValue. 始终以这种方式检查使用 System.Convert 类执行的转换。Conversions that are performed with the System.Convert class are always checked in this manner.

下表列出了使用 System.Convert 抛出 OverflowException 的转换,或要转换类型的值不在生成类型的定义范围内的任何已检查转换。The following table lists conversions that throw an OverflowException using System.Convert or any checked conversion if the value of the type being converted is outside the defined range of the resulting type.

类型Type 可转换为Can be converted to
Byte SByte
SByte Byte, UInt16, UInt32, UInt64Byte, UInt16, UInt32, UInt64
Int16 Byte, SByte, UInt16Byte, SByte, UInt16
UInt16 Byte, SByte, Int16Byte, SByte, Int16
Int32 Byte, SByte, Int16, UInt16,UInt32Byte, SByte, Int16, UInt16,UInt32
UInt32 Byte, SByte, Int16, UInt16, Int32Byte, SByte, Int16, UInt16, Int32
Int64 Byte, SByte, Int16, UInt16, Int32,UInt32,UInt64Byte, SByte, Int16, UInt16, Int32,UInt32,UInt64
UInt64 Byte, SByte, Int16, UInt16, Int32, UInt32, Int64Byte, SByte, Int16, UInt16, Int32, UInt32, Int64
Decimal Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64
Single Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64
Double Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64

请参阅See also