Methods of Type Casting
Type casting has two methodologies that we will cover in detail with examples:
Implicit Type Casting
In implicit typecasting, we convert the smaller types into larger types. Like in the “double” data type, we assign an integer value. The implicit type casting is done by the compiler and it is an automatic method that the machine does. The smaller and larger types are categorized based on the number of bytes they reserve while in memory allocation.
The compiler can type cast the “char” to “int” and so on. The larger data type can store the smaller ones.
Explicit Type Casting
In explicit typecasting, we assign the larger type to the smaller type. We have to manually do this typecasting. In this type of casting, the smaller type stores the result of the larger types. The sequence of order is as follows:
The “double” data type is the larger one. To store “double” in “int”, we will do an explicit type casting.
Syntax:
The attribute “to_type” is the type to which we want to change the variable and round that with parenthesis. The “var_0” is the name of the variable whose type we want to change. By using the syntax, let’s change the type of a variable like (float) var_0. Let us say, “var_0” is an “integer” variable and we change it to “float” by doing its type casting with (float).
Built-In Methods
Type casting can be accomplished by utilizing the built-in techniques of C#. The built-in method which is Convert.Totype_name() is used. Here, the “type_name” is the name of the data type that we want for a variable. Write the name of that variable inside the parenthesis like Convert.ToString(var_2). It converts the “var_2” to a “string” type.
Example 1:
In this example code, we will learn about the implicit typecasting that the complier itself does. We will typecast the smaller types into larger types.
class Program_1
{
static void Main() {
int value_0 = 76;
float value_1 = 78.9f;
double value_2 = value_1;
long value_3 = value_0;
Console.WriteLine("Implicit type casting from float to double "+ value_2);
Console.WriteLine("Implicit type casting from int to long "+ value_3);
}
}
The declaration of one integer type variable and one floating point variable is done in the first statement of the code. Then, initialize two more variables – one has a “double” type and the other one has a “long” type. Here, we want to change the type of “float” to “double”. It is done by the compiler. We just store the variable name which has a “float” type in the “double” variable like “double value_2 = value_1”. The “value_1” variable has a “float” type and is a smaller type. The “value_2” has a “double” type and it is the larger type. We just store the float value in the “double” variable. The compiler converts it to double. In the next statement, we assign the variable name having an “integer” data type to the “long” type variable. Hence, the compiler does an implicit type casting of integer to long. Next, display both the variables, the “double” type and the “long” type with a message by employing the Console.WriteLine() method. Here, we concatenate the message and the variable to represent the result on the console.
Example 2:
Here, we modify the variable type by explicit typecasting.
class Program_2
{
static void Main() {
float var_1 = 45.333f;
int var_2 = (int) var_1;
double var_3 = 77777.654336;
float var_4 = (float) var_3;
Console.WriteLine("Explicit type casting from float to int "+ var_2);
Console.WriteLine("Explicit type casting from double to float "+ var_4);
}
}
Let’s initialize four different variables. The first variable “var_1” has a float data type. The second one belongs to an integer type. Here, we assign the float type variable “var_1” to the integer type variable “var_2” and this is done by explicit type casting. Write the type that we want using the parenthesis, (int) var_1. This shows that we want to change the type of “var_1” as an integer. The third variable of data type ”double” is declared and a floating-point value is assigned to it. Furthermore, the explicit type casting is applied on “var_4”. The fourth variable shows the message and the values on the console with the use of Console.WriteLine(). The “float” value is changed to “integer” by getting the value before the decimal part. And the value of “double” is updated to “float” value after type casting. It represents the first part before the decimal point and only two decimal point values after rounding off.
Example 3:
This code explains the typecasting of the variable using the built-in methods in C#.
class Program_3
{
static void Main() {
float value_0 = 25.764f;
Console.WriteLine("From float to Byte "+ Convert.ToByte(value_0));
Console.WriteLine("From float to String "+ Convert.ToString(value_0));
Console.WriteLine("From float to Double "+ Convert.ToDouble(value_0));
Console.WriteLine("From float to Boolean "+ Convert.ToBoolean(value_0));
Console.WriteLine("From float to Decimal "+ Convert.ToDecimal(value_0));
Console.WriteLine("From float to int "+ Convert.ToInt32(value_0));
}
}
First, declare a float variable and initialize it with a floating point value. Then, employ the Console.WriteLine() function to display the text and the converted values. Within this function, by calling the Convert.ToByte(value_0), it converts the “float” value to “byte” after rounding it off. The ToString() converts the required data type to “string” without changing anything. It stores the value as a string. The ToDouble() method chanegs the “float” type to the “double” type. Then, convert the “float” to “Boolean”, “decimal”, and “integer”. We utilize the ToBoolean(), ToDecimal(), and ToInt32() methods. We can modify the data type of any variable by the use of these built-in functions.
Conclusion
We discussed how to do typecasting in C# language. Changing the type of a variable is known as type casting. There are two techniques of typecasting – one is implicit type casting where the compiler automatically changes the data type, and the other one is explicit type casting which we do manually. The explicit type casting has two methodologies: one is by mentioning the data type in parenthesis and placing it before the variable whose type we want to change. The other one is to utilize the built-in functions that are applied to modify the type of variables. The article explains everything about typecasting with practical examples.