Freigeben über


Type Conversion Tables

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Widening conversion occurs when a value of one type is converted to another type that is of equal or greater size. For example, the conversion of a Byte to an Int16 value is a widening conversion because all possible Byte values lie within the range of Int16.MinValue to Int16.MaxValue. A narrowing conversion occurs when a value of one type is converted to a value of another type that is of a smaller size. For example, the conversion of an Int64 to an Int32 value is a narrowing conversion because some possible Int64 values lie outside the range of Int32.MinValue to Int32.MaxValue. 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, 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, Double, Decimal

Int64

Decimal

UInt64

Decimal

Single

Double

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

Single, Double

UInt64

Single, Double

Decimal

Single, Double

Narrowing Conversions

A narrowing conversion to Single or Double can cause a loss of information. If the target type cannot properly express the magnitude of the source, then the resulting type is set to the constant PositiveInfinity or NegativeInfinity. The value of PositiveInfinity is the result of dividing a positive number by zero and is returned when the value of a Single or Double exceeds the value of the MaxValue field. The value of NegativeInfinity is the result of dividing a negative number by zero and is returned when the value of a Single or Double falls below the value of the MinValue field. A 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. However, if the value of a type 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, then an OverflowException is thrown. Conversions performed with the System.Convert class are always checked in this manner.

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 target type.

Type

Can be converted to

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