We often come across a situation where we want to convert a data of one type to another type before it is used in arithmetic operations.The process of converting data of one type to data of another type is known as type conversion. For example
byte b1 = 50;
byte b2 = 60;
byte b3 = b1 + b2;
This code attempts to add two byte values and store it in a third byte variable. But this will not work the compiler will throw an error message
“ cannot implicitly convert type int to type byte “
We are not using an int type values in the code then why are we facing this error message. This is because when we add two byte values, the compiler automatically converts them into int types and the result of the addition is also an int, not another byte.
Why should the compiler do that? This is because the byte resulting from the addition of two bytes is much larger than the range of byte. Since the result is of type int we cannot assign it to a byte variable thus we have to convert it back to the byte data type ,if we want to store it as a byte value or else we can say:
int b3 = b1 + b2; // no error
In c# type conversions takes place in two ways:
- Implicit conversion
- Explicit conversion
Implicit conversion in C#
Implicit conversion are those that will always succeed. That is, the conversion can always be performed without any loss of data. For numeric types, this implies that the destination type can fully represent the range of source type. For example, a short can be converted implicitly to an int, because the short range is a subset of the int range . Therefore
short b = 75;
int a = b; //implicit conversion
are valid statements . c# does the conversion automatically. This implicit conversion is also known as automatic type conversion. This process of assigning a smaller type to a larger one is known as widening or promotion and that of assigning larger type to a smaller type is known as narrowing. Narrowing may result in loss of information. For example, implicit conversion are possible in the following cases
- From byte to decimal
- From uint to double
- From ushort to long
Note that the conversions in these cases take place in a single operation, not by converting them in stages
Explicit conversion in C#
There are many conversions that cannot be implicitly made between types. If we attempt such conversions the compiler will throw an error message. For example the following conversion cannot be made implicit:
- int to short
- int to uint
- unit to int
- float to int
- decimal to any numeric type
- any numeric type to character
However we can explicitly carry out these conversions using the ‘cast’ operator. This process is known as casting and is done as follows:
type variable1 = (type) variable2;
The destination type is placed in parentheses before the source variable.
Examples:
int m = 50;
byte n = (byte)m;
long x = 1234L;
int y = (int ) x;
float f = 50.0F;
long y = (long) f;
casting is often necessary when a method returns a type other than the one we require. Casting into a smaller type may result in loss of data. For example, casting floating point value to an integer will result in the loss of the fractional part.
Examples
We can use casting to perform round-off floating point numbers to integers.
double a = 69.32;
double b = 89.66;
int x = (int) (a+0.5) //x = 69
int y = (int) (b+0.5) //y = 90
c# provides methods to convert between numeric values and strings
int m = 100;
string s = m. ToString();
This returns the string representation of 100 . That is , s contains the string ‘100’. Similarly we can do the reverse.
string s = “100”;
int n = int.Parse(s);
int m = n+50;
now the value of m would be 150
program that uses type casting in C#
using System; class Casting { Public static void Main() { Float sum; int i; sum = 0.0F; for (int i = 1; i <= 4 ; i++) { sum = sum+1 / (float) i; Console.WriteLine(“ i = “ + i); Console.WriteLine(“ sum = “ + sum); } } }
Output
i = 1 sum = 1 i = 2 sum = 1.5 i = 3 sum = 1.83333 i = 4 sum = 2.083333
in the above code we have to force the type conversion on anyone of the operands of an expression.