C# Type Conversion is a mechanism to convert one data type value to another one. Type conversion is possible if both the data types are compatible to each other; otherwise you will get an InvalidCastException. It is also called Type Casting. In C# Type Conversion is consist of 2 major forms:
- Explicit type conversion
- Implicit type conversion
using System; namespace TypeConversion { public class ExplicitConversion { public static void Main(string[] args) { int i; double d = 54.98; // double to int i = (int)d; Console.WriteLine(i); } } }
Explicit type conversion
Explicit conversions require a cast operator and it is done explicitly by users using the pre-defined functions.
For example, a byte can hold up to 255, and an int can hold up to 2 billion. We can implicitly convert a byte to an int because the value of 255 will always be able to fit into an int
using System; namespace TypeConversions { class Program { static void Main(string[] args) { byte b = 25; int i = b; Console.WriteLine(i); Console.ReadLine(); } } }
An int is big enough to store any value that byte has, so we can easily copy a byte to an int.
In situations where the compiler is sure that no data loss will happen, it will allow implicit conversion.
Implicit type conversion
This conversion type is known for its type-safe manner conversion. Conversions like from smaller to larger integral types and conversions from derived classes to base classes.
For example, if we try to convert an int with a value greater than 255 to a byte, it will cause data loss because a byte can only store up to 255.
using System; namespace TypeConversions { class Program { static void Main(string[] args) { int i = 300; byte b = i; Console.WriteLine(b); Console.ReadLine(); } } }
A byte is not big enough to store the value of 300 without data loss, so it can’t be implicitly converted and the compiler won’t allow it.
We need to explicitly tell the compiler that we’re aware of the data loss, and still want to go ahead with the conversion. Explicit type conversion is also known as casting.
C# Type Conversion Methods:
In C#, we have many built-in type conversion methods. Some are given below,
Methods | Description |
---|---|
1-ToByte | This method is used to convert a type to a byte. |
2-ToString | This method is used to convert a type to a strings |
3-ToType | This method is used to convert a type to a specified type. |
4-ToInt16 | This method is used to convert a type to a 16-bit integer. |
5-ToInt32 | This method is used to convert a type to a 32-bit integer. |
6-ToInt64 | This method is used to convert a type to a 64-bit integer. |
7-ToDouble | This method is used to convert a type to a double type. |
8-ToSbyte | This method is used to convert a type to a signed byte type. |
9-ToUInt16 | This method is used to convert a type to an unsigned int type. |
10-ToUInt32 | This method is used to convert a type to an unsigned long type. |
11-ToUInt64 | This method is used to convert a type to an unsigned big integer. |
12-ToSingle | This method is used to convert a type to a small floating-point number. |
13ToBoolean | This method is used to convert a type to a Boolean value, where possible. |
14-ToChar | This method is used to convert a type to a single Unicode character, where possible. |
15-ToDecimal | This method is used to convert a floating-point or integer type to a decimal type. |
16-ToDateTime | This method is used to convert a type (integer or string type) to date-time structures. |
using System; namespace TypeConversion { public class StringConversion { public static void Main(string[] args) { float f = 79.8f; int i = 54 ; bool b = true; Console.WriteLine(i.ToString()); Console.WriteLine(f.ToString()); Console.WriteLine(b.ToString()); } } }
What is the Upcasting and Downcasting?
There are two more casting terms Upcasting and Downcasting. basically these are parts of Implicit conversion and Explicit conversion.
Implicit conversion of derived classes to base class is called Upcasting
and Explicit conversion of base class to derived classes is called Downcasting
.
class Base { public int num1 { get; set; } } class Derived : Base { public int num2 { get; set; } } class Program { static void Main(string[] args) { Derived d1 = new Derived(); //Upcasting Base b1 = d1; Base b2 = new Base(); //Downcasting Derived d2 = (Derived)b2; } }