In this tutorial, you will learn about C Sharp type casting to change the type of variables. Casting types are implicit and explicit to convert the variable from one type to others.
What is Type Casting in C Sharp (C#)
Type casting is the way of assigning a value of one data type to another data type. After we create a variable in C#, we cannot create it again. However, there are some situations where we need to change the data type.
If the conversion is compatible with the data type, it can be automatically convertible to the other data type in C#. If the conversion is not with the data type, you need to manually convert it to another data type.
Types of Casting in C Sharp (C#)
There are two types of casting in C# that are given below:-
- Implicit Type Casting (Automatic conversion from smaller to larger data types)
- Explicit Type Casting (Manual conversion from larger to smaller data types)
Let’s find out these types in detail given below with examples.
Implicit Type Casting in C Sharp (Automatic Conversion C#)
In an implicit type casting, one data type is automatically converted to another data type during compilation in C#. This type is safe to use and you will not lose any data.
To convert the data type implicitly, you have to assign the smaller variable to the larger variable. You can mainly use implicit type casting to convert smaller data types to larger data types.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
using System; namespace Demo { class Program { static void Main(string[] args) { int myNum = 11; //Implicit conversion double myNumDbl = myNum; //Value before conversion Console.WriteLine("myNum value is: "+myNum); //Value after conversion Console.WriteLine("myNumDbl value is: "+myNumDbl); } } } |
Output
myNumDbl value is: 11
Explanation
In the above example, we have first created an integer variable myNum
using int
data type.
1 |
int myNum = 11; |
After that, we have created the double
data type variable with the name myNumDbl
. We have also assigned its value as an integer variable myNum
which is an implicit conversion.
1 2 |
//Implicit conversion double myNumDbl = myNum; |
On C# compilation, the value of int
data type automatically converted to the double
data type.
As it’s an implicit conversion from a smaller data type to a larger data type, there will be no data loss after conversion. int
is the smaller data type and double
is the larger data type in C#.
Explicit Type Casting in C Sharp (Manual Conversion C#)
In explicit data type, one data type is manually converted to another data type at the time of compilation in C#. If the types are not compatible with the conversion, you will get some errors during compilation. This data type is not safe to use as there is some data loss in the output.
To explicitly convert one data type to another, you have to place the data type before the smaller variable within the parenthesis. See the example given below to learn the method.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
using System; namespace Demo { class Program { static void Main(string[] args) { double myNumDbl = 9.48; //Explicit conversion int myNum = (int) myNumDbl; //Value before conversion Console.WriteLine("myNumDbl value is: "+myNumDbl); //Value after conversion Console.WriteLine("myNum value is: "+myNum); } } } |
Output
myNum value is: 9
The above example converts the larger data type double
to the smaller data type int
.
Explanation
Let’s understand the process of explicit conversion. We have to first create the larger data type double
as given below.
1 |
double myNumDbl = 9.48; |
After that, we can create a smaller data type int
and assign the larger data type variable as its value. You also have to place the int
data type before the larger data type within the parenthesis as given below.
1 2 |
//Implicit conversion int myNum = (int) myNumDbl; |
On C# compilation, you will get the result as given below. The result shows some data loss after you get the output.
1 2 |
myNumDbl value is: 9.48 myNum value is: 9 |
As it is an explicit conversion, you need to manually assign the data type with the larger variable. Also, there will be some data loss at the end of the result.
C Sharp Built-in Methods for Type Conversion
C-Sharp (C#) has some built-in methods that are useful to convert one data type to another. Below is the list of the useful method for type conversion:-
Method Name | Description |
---|---|
ToBoolean |
Convert a type to a boolean value |
ToChar |
To convert a type to character type |
ToString |
To convert a type to string type |
ToByte |
Convert a type to a byte value |
ToDecimal |
For converting a type to a decimal value |
ToDouble |
Convert a type to double value |
You can use these simple methods to easily convert your data type to the required data type.
Boolean Type Conversion Using ToBoolean
If you want to convert your required data type to boolean, you can use the C# built-in method Convert.ToBoolean()
. You have to pass the variable as the parameter of the method to convert its type to boolean data type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; namespace Demo { class Program { static void Main(string[] args) { //Declare integer variable int myNum = 21; //Convert integer to boolean type bool b = Convert.ToBoolean(myNum); //Value after conversion Console.WriteLine("The boolean value of "+myNum+" is: "+b); } } } |
Output
In the above example, the value is converted to boolean. If the value is 0
, it is converted to a boolean value as False
. If the value is other than 0, it is converted to a boolean value as True
.
String Type Conversion Using ToString
The string type conversion can be done using the C# built-in method Convert.ToString()
. You have to pass the variable as its parameter to convert it into a string type value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; namespace Demo { class Program { static void Main(string[] args) { //Declare integer variable int myNum = 29; //Convert integer to string type string myStr = Convert.ToString(myNum); //Value after conversion Console.WriteLine("The string value of "+myNum+" is: "+myStr); } } } |
Output
The above output shows that the converted value is string type.
Double Type Conversion Using ToDouble
If you want to convert the value to double data type, you have to use the C-Sharp built-in method Convert.ToDouble()
. Pass the variable as its argument to change its value to the double data type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; namespace Demo { class Program { static void Main(string[] args) { //Declare integer variable int myNum = 32; //Convert integer to double type double myDbl = Convert.ToDouble(myNum); //Value after conversion Console.WriteLine("The double value of "+myNum+" is: "+myDbl); } } } |
Output
The above value shows that the converted value is a double data type.
Related Posts
Reference