发生扩大转换。,当一个类型的值转换为相等或更大范围的另一种类型时。 收缩转换时,会发生一个类型的值转换为较小的另一种类型的值时。 本主题中的表阐释了两种转换类型显示的行为。
下表描述了可以执行,不会丢失信息的扩大转换。
类型 |
可以转换,而不会丢失数据 |
Byte |
UInt16, Int16, UInt32, Int32, UInt64, Int64, Single, Double, Decimal |
SByte |
Int16, Int32, Int64, Single, Double, Decimal |
Int16 |
Int32, Int64, Single, Double, Decimal |
UInt16 |
UInt32, Int32, UInt64, Int64, Single, Double, Decimal |
Char |
UInt16, UInt32, Int32, UInt64, Int64, Single, Double, Decimal |
Int32 |
Int64, Double, Decimal |
UInt32 |
Int64, UInt64, Double, Decimal |
Int64 |
Decimal |
UInt64 |
Decimal |
Single |
Double |
为 Single 或 Double 的那些扩大转换可能会导致丢失精度。 下表描述了有时会导致信息丢失的扩大转换。
类型 |
可以转换 |
Int32 |
Single |
UInt32 |
Single |
Int64 |
Single, Double |
UInt64 |
Single, Double |
Decimal |
Single, Double |
为 Single 或 Double 的收缩转换可能导致信息丢失。 如果目标类型无法正确表达源的大小,则结果类型设置为常数 PositiveInfinity 或 NegativeInfinity。 ,当 Single 或 Double 的值超过 MaxValue 字段的值时,PositiveInfinity 由部件的正数由零和也会返回。 ,当 Single 或 Double 的值。 MinValue 字段中的值时,会NegativeInfinity 产生负数除以零时和也会返回。 从 Double 的转换到 Single 可能导致 PositiveInfinity 或 NegativeInfinity。收缩转换可能会导致其他数据类型的信息丢失。 但是, OverflowException 引发,如果将类型的值不在目标类型的 MaxValue 和 MinValue 字段指定的范围内,因此,转换在运行时检查确保目标类型的值不超过其 MaxValue 或 MinValue。 对 System.Convert 类的转换始终以这种方式检查。
下表列出了 OverflowException 使用 System.Convert 或所有签出的转换的转换,如果转换的类型的值不在结果类型的定义的大小。
类型 |
可以转换 |
Byte |
SByte |
SByte |
Byte, UInt16, UInt32, UInt64 |
Int16 |
Byte, SByte, UInt16 |
UInt16 |
Byte, SByte, Int16 |
Int32 |
Byte, SByte, Int16, UInt16, UInt32 |
UInt32 |
Byte, SByte, Int16, UInt16, Int32 |
Int64 |
Byte, SByte, Int16, UInt16, Int32, UInt32, UInt64 |
UInt64 |
Byte, SByte, Int16, UInt16, Int32, UInt32, Int64 |
Decimal |
Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64 |
Single |
Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64 |
Double |
Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64 |