An implicit conversion of variables is a kind of conversion that is automatically done by the compiler. A variable of one data type can be implicitly converted to another data type provided that its content can be handled by the type it will be converted to. For example, a bytedata type can hold values from 0 to 255. You convert this variable to an int type like this:
byte number1 = 5; int number2 = number1;
We initialize the value of number1 to 5 and use its value to initialize number2. The int variable number2 can hold the byte value 5 because int data type can hold the value 5 without any problems as it falls within the range of values an int can hold. Therefore, number1 which is a byte, is implicitly converted to number2 which is an int. What if we switch the types of the two?
int number1 = 5; byte number2 = number1;
This time, you will receive an error. Even though the value 5 of the int variable number1 falls in the range of values a byte can hold (1 – 255), the byte uses less memory than the int. The byte data type contains 8 bits or 8 digits of binary value while int contains 32 bits or binary digits. A binary number is a number that is composed of 0’s and 1’s. For example, we see the number 5 as 5 but the computer interprets it as a binary number 101. So when we store 5 in a byte variable, it will be represented as this:
And if we place it in an int, the binary representation of it will be as follows:
Therefore, placing an int value to a byte variable is like trying to shoot a bowling ball in a golf hole. You can still assign a byte variable with an int value by using explicit conversion which is discussed in the next lesson.
Another thing to take note about is that you cannot convert floating point numbers or numbers with decimal part implicitly into a number that accepts a whole number such as int. This is because of the reason that you will lose the fractional part of the floating-point value.
double number1 = 5.25; int number2 = number1; //Error
You can also convert a char into a ushort because these two types has the same range of values they can store. Although each of them is interpreted differently by the compiler. The char is interpreted as a character and a ushort is interpreted as a number.
char charVar = 'c'; ushort shortVar = charVar; Console.WriteLine(charVar); Console.WriteLine(shortVar);
Below is a table showing the numeric conversions the compiler can do implicitly:
|Source Type||Can Safely Be Converted To|
|byte||short, ushort, int, uint, long, ulong, float, double, decimal|
|sbyte||short, int, long, float, double, decimal|
|short||int, long, float, double, decimal|
|ushort||int, uint, long, ulong, float, double, decimal|
|int||long, float, double, decimal|
|uint||long, ulong, float, double, decimal|
|long||float, double, decimal|
|ulong||float, double, decimal|
|char||ushort, int, uint, long, ulong, float, double, decimal|
Often, there is an ambiguity to what would be the type of a data. For example, how can we know if number 7 is int, uint, long, or ulong? We can append characters to the end of a number like this:
uint number1 = 7U; long number2 = 7L; ulong number3 = 7UL;
The compiler will treat it as an int if no character is appended to the number. Also, note that the casing doesn’t matter and you can use u,l and ul.
By default number with a decimal point is considered as double. You can indicate the it is a float by using the F character, and M if you want to treat it as a decimal.
double number1 = 1.23; float number2 = 1.23F; decimal number3 = 1.23M;