C# allows us to convert the variables of one data type to another. This conversion referred to as type conversion or type casting. There are various ways to typecast variables, casting can be between a larger size type to a smaller size type or vice-versa.
There are two types of casting in C#:
- Implicit Casting (automatic casting): Conversion of smaller type to a larger.
char
->int
->long
->float
->double
- Explicit Casting (manual casting): Conversion of a larger type to a smaller size type.
double
->float
->long
->int
->char
Implicit Casting
For this kind of conversion, there is no need for special syntax. Implicit includes the conversion of a smaller data type to a larger size data type and conversions from derived classes to base classes, so there is no loss of data here. It is also known as Automatic Type Conversion.
Although the two data types need to be compatible with each other that is the numeric data types are compatible with each other but the automatic conversion is not supported to the conversion of numeric type to char or boolean.
1 2 3 4 5 | //Example int num1 = 20; int num2 = 30; long result; result = num1 + num2; |
As the Implicit conversion is done by the compiler itself, compiler first checks the type compatibility before the conversion. The compatibility is checked in the following order:
byte -> short -> int -> long -> float -> double
The following shows the implicit types of conversion that is supported by C#:
Convert from | Convert to |
---|---|
byte | short, int, long, float, double |
short | int, long, float, double |
int | long, float, double |
long | float, double |
float | double |
Example of implicit type casting in C# programming.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | using System; namespace Conversion { class ImplicitConversion { static void Main(string[] args) { int num1 = 20; int num2 = 30; long result; result = num1 + num2; Console.WriteLine("Result: {0}", result); } } } |
Output:
1 | Result: 50 |
Explicit Casting
Explicit conversion is a manual conversion that it is done by the user themselves. We specifically do the explicit conversion to prevent data loss or maybe when the conversion is not succeeded and it is done by using a cast operator ()
.
This conversion is useful for incompatible data types where above automatic conversion cannot be done. This happens when data of a larger type is converted to data of a smaller type.
long double - > double -> float -> long -> short -> char
Example of explicit type casting in C# programming.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | using System; namespace Conversion { class ExplicitConversion { public static void Main(String[] args) { double db = 165.15; //Explicit Casting int num = (int) db; Console.WriteLine("Value of i: {0}", num); } } } |
Output:
1 | Value of i: 165 |
C# Type Conversion Methods
There are some built-in conversion methods provided by the C#.
Methods | Description |
---|---|
ToBoolean | This converts a type to a Boolean value, |
ToByte | It will convert a type to a byte. |
ToChar | It will convert a type to a single Unicode character. |
ToDateTime | It will convert a type (integer or string type) to date-time structures. |
ToDecimal | It will convert a floating-point or integer type to a decimal type. |
ToDouble | It will convert a type to a double type. |
ToInt16 | It will convert a type to a 16-bit integer. |
ToInt32 | It will convert a type to a 32-bit integer. |
ToInt64 | It will convert a type to a 64-bit integer. |
ToSbyte | It will convert a type to a signed byte type. |
ToSingle | It will convert a type to a small floating-point number. |
ToString | It will convert a type to a string. |
ToType | It will convert a type to a specified type. |
ToUInt16 | It will convert a type to an unsigned int type. |
ToUInt3 | It will convert a type to an unsigned long type. |
ToUInt64 | It will convert a type to an unsigned big integer. |
C# program to demonstrate some the built-in type conversion methods
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | using System; namespace Conversion { class MethodConversion { public static void Main(String[] args) { int i = 20; double d = 465.19; float f = 77.154 F; //uilt- In Type Conversion Console.WriteLine(Convert.ToDouble(i)); Console.WriteLine(Convert.ToString(f)); Console.WriteLine(Convert.ToInt32(d)); Console.WriteLine(Convert.ToUInt32(f)); } } } |
Output:
1 2 3 4 | 20 77.154 465 77 |