Type Casting In C#

What is Type Casting?

The meaning of Type Casting is to change one data type into another data type. Developers change data type according to their need. Let us understand this with the help of a real example.

Types of casting in C#

There are two types of conversion.

  1. Implicit Conversion
  2. Explicit Conversion

Implicit Conversion


Implicit Conversion is done by the compiler itself. Let us see the example how the compiler does the conversion. Before starting the discussion, let us focus on the UI of the application. I have taken 2 textboxes and two buttons to perform both types of conversion.

I wrote the code for implicit button click event. As you can see in the below code, I used two integer data types and one long data type and added two integer values and assigned to a long data type. Whenever we add these two integer numbers, it will give you result in the integer. In this, the integer values are implicitly converted to the long data type. You need not tell the compiler to do the conversion, it automatically does. When compiler does the conversion by itself, this type of conversion is known as implicit conversion.

  1. private void ImplicitClick_Click(object sender, EventArgs e)  
  2. {  
  3.     int firstValue = 25;  
  4.     int secondValue = 26;  
  5.     long result = firstValue + secondValue;  
  6.     //MessageBox.Show(result.ToString());         

Below table shows the implicit types of conversion that is supported by C#.

From To
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
Int long, float, double, decimal
Uint long, ulong, float, double, decimal
Long float, double, decimal
Ulong float, double, decimal
Float double
Char ushort, int, uint, long, ulong, float, double, decimal

Explicit Conversion


Explicit Conversion is done by the users according to their need/requirement. We tell the compiler to do the conversion. Let us see how we can do Explicit Conversion. I wrote the code on the cast button click event, as you can see below.
  1. private void btnCast_Click(object sender, EventArgs e)  
  2. {  
  3.     int firstName = int.Parse(txtFirstNumber.Text);  
  4.     int secondName = int.Parse(txtSecondNumber.Text);
  5.     MessageBox.Show((firstName + secondName).ToString());  
  6. }  

Here, my main aim is to add the values of both textboxes and show the result in the Message Box. As we know, the textbox returns string value and we can’t do addition operation on the string value. In order to perform the addition operation on the textboxes value, first, we will have to convert the textbox values to the integer type.

I used Parse Method to convert the string type to integer type. This Parse method is available inside the Int32 struct and this is a static method which returns an integer value. This type of conversion is known as the Explicit conversion because we forcefully told the compiler to convert a string type data to an integer type.
 
Again, I displayed the result in the message box. I used the Show method of messageBox class to display the result. Show method takes a string value and I have converted that integer value into string value by using ToString() method to display the result. The result can be seen in the below image.