Permalink
Fetching contributors…
Cannot retrieve contributors at this time
79 lines (69 sloc) 6.28 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic helpviewer_keywords ms.assetid caps.latest.revision author ms.author manager
Type Conversion Tables in .NET
03/30/2017
.net
dotnet-standard
article
widening conversions
narrowing conversions
type conversion, table
converting types, narrowing conversions
converting types, widening conversions
base types, converting
tables [.NET Framework], type conversions
data types [.NET Framework], converting
0ea65c59-85eb-4a52-94ca-c36d3bd13058
11
rpetrusha
ronpet
wpickett

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
xref:System.Byte xref:System.UInt16, xref:System.Int16, xref:System.UInt32, xref:System.Int32, xref:System.UInt64, xref:System.Int64, xref:System.Single, xref:System.Double, xref:System.Decimal
xref:System.SByte xref:System.Int16, xref:System.Int32, xref:System.Int64, xref:System.Single, xref:System.Double, xref:System.Decimal
xref:System.Int16 xref:System.Int32, xref:System.Int64, xref:System.Single, xref:System.Double, xref:System.Decimal
xref:System.UInt16 xref:System.UInt32, xref:System.Int32, xref:System.UInt64, xref:System.Int64, xref:System.Single, xref:System.Double, xref:System.Decimal
xref:System.Char xref:System.UInt16, xref:System.UInt32, xref:System.Int32, xref:System.UInt64, xref:System.Int64, xref:System.Single, xref:System.Double, xref:System.Decimal
xref:System.Int32 xref:System.Int64, xref:System.Double, xref:System.Decimal
xref:System.UInt32 xref:System.Int64, xref:System.UInt64, xref:System.Double, xref:System.Decimal
xref:System.Int64 xref:System.Decimal
xref:System.UInt64 xref:System.Decimal
xref:System.Single xref:System.Double

Some widening conversions to xref:System.Single or xref:System.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
xref:System.Int32 xref:System.Single
xref:System.UInt32 xref:System.Single
xref:System.Int64 xref:System.Single, xref:System.Double
xref:System.UInt64 xref:System.Single, xref:System.Double
xref:System.Decimal xref:System.Single, xref:System.Double

Narrowing Conversions

A narrowing conversion to xref:System.Single or xref:System.Double can cause a loss of information. If the target type cannot properly express the magnitude of the source, the resulting type is set to the constant PositiveInfinity or NegativeInfinity. PositiveInfinity results from dividing a positive number by zero and is also returned when the value of a xref:System.Single or xref:System.Double exceeds the value of the MaxValue field. NegativeInfinity results from dividing a negative number by zero and is also returned when the value of a xref:System.Single or xref:System.Double falls below the value of the MinValue field. A conversion from a xref:System.Double to a xref:System.Single might result in PositiveInfinity or NegativeInfinity.

A narrowing conversion can also result in a loss of information for other data types. However, an xref:System.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. Conversions that are performed with the xref:System.Convert?displayProperty=nameWithType class are always checked in this manner.

The following table lists conversions that throw an xref:System.OverflowException using xref:System.Convert?displayProperty=nameWithType 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
xref:System.Byte xref:System.SByte
xref:System.SByte xref:System.Byte, xref:System.UInt16, xref:System.UInt32, xref:System.UInt64
xref:System.Int16 xref:System.Byte, xref:System.SByte, xref:System.UInt16
xref:System.UInt16 xref:System.Byte, xref:System.SByte, xref:System.Int16
xref:System.Int32 xref:System.Byte, xref:System.SByte, xref:System.Int16, xref:System.UInt16,xref:System.UInt32
xref:System.UInt32 xref:System.Byte, xref:System.SByte, xref:System.Int16, xref:System.UInt16, xref:System.Int32
xref:System.Int64 xref:System.Byte, xref:System.SByte, xref:System.Int16, xref:System.UInt16, xref:System.Int32,xref:System.UInt32,xref:System.UInt64
xref:System.UInt64 xref:System.Byte, xref:System.SByte, xref:System.Int16, xref:System.UInt16, xref:System.Int32, xref:System.UInt32, xref:System.Int64
xref:System.Decimal xref:System.Byte, xref:System.SByte, xref:System.Int16, xref:System.UInt16, xref:System.Int32, xref:System.UInt32, xref:System.Int64, xref:System.UInt64
xref:System.Single xref:System.Byte, xref:System.SByte, xref:System.Int16, xref:System.UInt16, xref:System.Int32, xref:System.UInt32, xref:System.Int64, xref:System.UInt64
xref:System.Double xref:System.Byte, xref:System.SByte, xref:System.Int16, xref:System.UInt16, xref:System.Int32, xref:System.UInt32, xref:System.Int64, xref:System.UInt64

See Also

xref:System.Convert?displayProperty=nameWithType
Type Conversion in .NET