What is the meaning of type Casting ?.
The meaning of type casting is to change from one data type to another data type. Developers change data type according to their need. Let us understand with the help of real example why we need to use type casting.
There are two types of casting in c#
There are two types of conversions:
- Implicit Conversion
- Explicit Conversion
- Implicit Conversion:- Implicit Conversion is done by the compiler by itself. Let us see the example how the compiler does the conversion .Before starting the discussion let us focus on UI of application. I have taken 2 textboxes and two buttons to perform both types of conversion.
I wrote code on implicit button click event. As you can 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 will add two integer numbers it will give you result in the integer. In this, the integer values are implicitly converted to long data type.You need not tell compiler to do the conversion, it automatically does. When compiler does the conversion by itself this type of conversion is known as implicit conversion.
private void ImplicitClick_Click(object sender, EventArgs e)
int firstValue = 25;
int secondValue = 26;
long result = firstValue + secondValue;
Below table shows the implicitly type conversions that are supported by C#:
|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|
|Char||ushort, int, uint, long, ulong, float, double, decimal|
- Explicit Conversion:- Explicit Conversion is done by the user according to their need/requirement. We tell to do conversion to the compiler. Let us see we can do Explicit Conversion. I wrote code on the cast button click event as you can see below.
private void btnCast_Click(object sender, EventArgs e)
int firstName = int.Parse(txtFirstNumber.Text);
int secondName = int.Parse(txtSecondNumber.Text);
MessageBox.Show((firstName + secondName).ToString());
Here my main aim is to add the values of both textboxes and show the result in the Message Box. As we know 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 textboxes values to the integer type. I used Parse Method to convert string type to integer type, this Parse method is available inside the Int32 struct and this is a static method which retursn integer value. This type of conversion is known as Explicit conversion , because we forcefully told to complier to convert string type data to integer type. Again I displayed result on the message box, I used the Show method of messageBox class to display the result. Show method takes string value and I converted integer value to string value by using ToString() method to display result. Result as you can see in the below image.