As we discussed in the last lesson, implicit type conversion (hidden conversion) is only possible when there is no risk of any data loss during the transformation (for instance, when converting a lower range number, like an int, to a higher range number, like a long). The reason why this kind of transformation is called implicit is because you don’t need any operator for it, it is performed automatically.
The following table displays the possible implicit conversions for primitive data types:
- sbyte → short, int, long, float, double, decimal;
- byte → short, ushort, int, uint, long, ulong, float, double, decimal;
- short → int, long, float, double, decimal;
- ushort → int, uint, long, ulong, float, double, decimal;
- char → ushort, int, uint, long, ulong, float, double, decimal
- uint → long, ulong, float, double, decimal;
- int → long, float, double, decimal;
- long → float, double, decimal;
- ulong → float, double, decimal;
- float → double.
There is a single thing to be mentioned here. When we talked about real numbers in C#, I explained that an integer uses all its bits to store the number value, while a float uses a part of its bits to store the fractional part. Therefor, when converting an int to a float, the int can be rounded up and a loss of precision can appear. Same thing applies for conversion from type long to double.
The concepts explained in this lesson are also shown visually as part of the following video: