 .NET 1.1C# 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 it is often necessary to assign a value from one variable to another. This may be done directly or as a part of 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 value being assigned to it. A data type is considered 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 the implicit type conversion is invalid then 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
The automatic conversion of types offered by the implicit conversion system is useful for many purposes. However, it is often necessary to perform a type conversion between incompatible types. As an example, it is possible that a value with a decimal part is transferred into an integer variable. 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 between incompatible data types is to be performed. The cast operation forces the conversion, possibly losing some data accuracy along the way. Care must be taken as it is possible to cause run-time errors or to receive unpredictable results using casts when the destination variable's data 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 casts in action:
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;
|