Explicit Conversion

[print-me]

Explicit conversion lets you force the program to convert a data into another data type if it doesn’t support implicit conversion. The explicit conversion has a tendency of losing or resulting to a modification of value so be cautious when using this type of conversion. We do an explicit conversion in C# by doing a cast. Casting is just another term for explicit conversion and it follows the following syntax:

datatypeA variableA = value;
datatypeB variableB = (datatypeB)variableA;

datatypeA is the type of the source data. datatypeB is the destination type. When doing a cast, we enclosed the target data type between parenthesis followed by the variable of another data type that we are going to convert. For example, we saw that you cannot implicitly convert an int into a byte. It’s like downgrading the int variable. But we can use casting to have a successful conversion.

int number1 = 5;
byte number2 = (byte)number1;

If you compile this code, you will not receive an error. We have “downgraded” an int data into a byte data. As I have said earlier, there are events where you can lose the originality of data you are converting. An example is when converting a floating-point number such as double in an int which can only hold whole numbers.

double number1 = 5.25;
int number2 = (int)number1;
Console.WriteLine(number2);
5

The code above will give as an output of 5 instead of 5.25. The fractional part or the part after the decimal point is lost because int data type cannot hold it. Another thing to keep in mind is if you are converting a variable containing a value which is not within the range of the values a target variable can hold, then odd results will be encountered. Consider the following code.

int number1 = 300;
byte number2 = (byte)number1;
Console.WriteLine("Value of number2 is {0}.", number2);
Value of number2 is 44.

The output below shows that converting the int value 300 into byte resulted to 44. Byte can only hold a value 0 to 255. So why did we get 44 instead of 300? This has something to do with the number of bits each type can hold. byte can hold 8 bits (0’s or 1’s) and int can hold 32. If we look at the binary representation of the two values, we can see why we got 44.

300 = 00000000000000000000000100101100 
255 =                         11111111 
 44 =                         00101100

The representation above shows that the max value of byte which is 255 takes up eight bits (11111111) so only the first eight bits of the int value is transferred to the byte variable which is 00101100 or 44 in decimal representation. Fitting a data into a variable that cannot hold the value results to an overflow. Overflows can also occur if the result of a mathematical expression is assigned to a variable that cannot hold a result. This is called arithmetic overflow.

byte sum = (byte)(150 + 150);

Even though we will lost some data because of the conversion, the compiler still accepts our code. To make the program issue an error when an expression results to an overflow, you can use the checked keyword.

int number1 = 300;
byte number2 = checked((byte)number1);
Console.WriteLine("Value of number2 is {0}.", number2);

Unhandled Exception: System.OverflowException: Arithmetic operation resulted in an overflow …

The program will throw a System.OverflowException which simply means that an overflow was detected. This will prevent the program to progress.