This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

.NET 1.1+

# C# Numeric Data Type Conversion

The fifth part of the C# Fundamentals tutorial continues the discussion of the numeric types available to the C# programmer. In this instalment, we investigate the conversion of numbers from one numeric type to another.

## What is Type Conversion?

When developing software you will often assign a value from one variable to another. This may be done directly or as during a calculation. If the variables involved in the assignment differ in type, conversion between the types is required. This conversion can be achieved implicitly, if the data types are compatible, or explicitly, if they are not.

## Implicit Conversion

Implicit conversion of data types takes place automatically when the data type being assigned to is compatible with the its new value. A data type is compatible if all of the possible values it can hold can also be held by the receiving variable. For example, an int can be implicitly converted to a double. The reverse is not true as an int may not be able to hold a double value without truncating the fractional part.

When types are compatible, you may simply assign one value to another. If implicit type conversion is invalid the compiler will raise an error and the code will not compile. The following examples illustrate this:

```int iInteger = 1;
long lLongInteger = 2;
double dDoublePrecision = 3.456;

// An int can be implicitly converted into a long
lLongInteger = iInteger;

// A double can receive an int
dDoublePrecision = iInteger;

// A long may be larger than the maximum int size so this is invalid
iInteger = lLongInteger;

// A double may be truncated if assigned to a long so this is invalid
lLongInteger = dDoublePrecision;```

## Explicit Casting

Implicit conversion is useful for many purposes. However, it is often necessary to perform type conversion between incompatible types. For example, it is possible that a value with a decimal part be transferred into an integer. Although the integer variable cannot physically hold the fractional part of the value, you can perform this conversion knowing that the value will be truncated.

A cast instruction tells the compiler that an explicit conversion is to be performed. The cast operation forces the conversion, possibly losing some data along the way. Care must be taken, as it is possible to cause run-time errors or receive unpredictable results using casts when the destination variable's type is too small to receive a large value.

The cast operator is added before the value being explicitly converted. The operator consists of the destination data type in parentheses (). The following code samples show some casts:

```int iInteger = 1;
long lLongInteger = 2;
double dDoublePrecision = 3.456;
long lTooBigForInt = 9999999999;

// Explicitly convert long to int
iInteger = (int)lLongInteger;

// Explicitly convert a double to an int.  The fractional part is truncated.
iInteger = (int)dDoublePrecision;

// Although accepted by the compiler, this generates an unpredictable
// result because the value is too large to be assigned to an int
iInteger = (int)lTooBigForInt;```
13 August 2006