C# Type Casting
What is type casting
Typecasting, or type conversion, is a method of changing an variable from one data type to another data type.
An example of typecasting is converting an integer to a string. This might be done in order to compare two numbers, when one number is saved as a string and the other is an integer.
In C# type conversion is of two types as shown in figure:
Necessity for Type Casting
- It is used in computer programming to ensure variables are correctly processed by a function.
- When working with user input
How Does Type Casting Work
The compiler will automatically change one type of data into another if it makes sense. For instance, if you assign an integer value to a floating-point variable, the compiler will convert the int to a float.
Difference Between Implicit and Explicit Type Conversion
| Implicit Type Conversion | Explicit Type Conversion |
|---|---|
| An implicit type conversion is automatically performed by the compiler when differing data types are intermixed in an expression. | An explicit type conversion is user-defined conversion that forces an expression to be of specific type. |
| An implicit type conversion is performed without programmer’s intervention. | An explicit type conversion is specified explicitly by the programmer. |
| Example: a, b = 5, 25.5 c = a + b | Example: a, b = 5, 25.5 c = int(a + b) |
Implicit Conversion(automatically)
If a compiler converts one type of data into another type of data automatically, it is known as implicit conversions. There is no data loss in implicit conversion.
In implicit casting, conversion always takes place from lower to higher. Implicit is always under control of CLR. Here is an example of implicit conversion:
char -> int -> long -> float -> double [ converting a smaller type to a larger type size ]
short a = 20; //Implicit conversion int b = a;
int myInt = 9; double myDouble = myInt; // Automatic casting: int to double Console.WriteLine(myInt); // Outputs 9 Console.WriteLine(myDouble); // Outputs 9
Explicit Conversion(manually)
When the data of one type is converted explicitly to another type with the help of some pre-defined functions it is called as explicit conversion and there may be data loss in this process because the conversion is forceful. These are some of the conversions that cannot be made implicit:
- int to short
- int to uint
- uint to int
- float to int
double -> float -> long -> int -> char [ converting a larger type to a smaller size type ]
double myDouble = 9.78; int myInt = (int) myDouble; // Manual casting: double to int Console.WriteLine(myDouble); // Outputs 9.78 Console.WriteLine(myInt); // Outputs 9
Some Type Conversion Methods
It is also possible to convert data types explicitly by using built-in methods, such as Convert.ToBoolean, Convert.ToDouble, Convert.ToString, Convert.ToInt32 (int) and Convert.ToInt64 (long):
int myInt = 10; double myDouble = 5.25; bool myBool = true; Console.WriteLine(Convert.ToString(myInt)); // convert int to string Console.WriteLine(Convert.ToDouble(myInt)); // convert int to double Console.WriteLine(Convert.ToInt32(myDouble)); // convert double to int Console.WriteLine(Convert.ToString(myBool)); // convert bool to string