Explicit numeric conversions table

Domains: C#

The following table shows the predefined explicit conversions between .NET numeric types for which there is no implicit conversion.

 

From  To
sbyte                          byteushortuintulong, or char
byte sbyte or char
short sbytebyteushortuintulong, or char
ushort sbytebyteshort, or char
int sbytebyteshortushortuintulong,or char
uint sbytebyteshortushortint, or char
long sbytebyteshortushortintuintulong, or char
ulong sbytebyteshortushortintuintlong, or char
char sbytebyte, or short
float sbytebyteshortushortintuintlongulongchar,or decimal
double sbytebyteshortushortintuintlongulongcharfloat,or decimal
decimal  sbytebyteshortushortintuintlongulongcharfloat, or double

Remarks


  • The 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. Otherwise, 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.
  • When you convert a decimal 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, an OverflowException is thrown.
  • 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. In a checked context, an OverflowException is thrown, while in an unchecked context, the result is an unspecified value of the destination type.
  • When you convert double to float, the double value is rounded to the nearest float value. If the double value is too small or too large to fit into the destination type, the result will be zero or infinity.
  • When you convert float or double to decimal, the source value is converted to decimalrepresentation 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:
  • If the source value is too small to be represented as a decimal, the result becomes zero.
  • If the source value is NaN (not a number), infinity, or too large to be represented as a decimal, an OverflowException is thrown.
  • When you convert decimal to float or double, the decimal value is rounded to the nearest double or float value.
Page structure