C# – Type Conversion
Type conversion is converting one type of data to another type. It is also known as Type Casting. In C#, type casting has two forms −
- Implicit type conversion − These conversions are performed by C# in a type-safe manner. For example, are conversions from smaller to larger integral types and conversions from derived classes to base classes.
- Explicit type conversion − These conversions are done explicitly by users using the pre-defined functions. Explicit conversions require a cast operator.
The following example shows an explicit type conversion −
using System; namespace TypeConversionApplication { class ExplicitConversion { static void Main(string[] args) { double d = 5673.74; int i; // cast double to int. i = (int)d; Console.WriteLine(i); Console.ReadKey(); } } }
When the above code is compiled and executed, it produces the following result −
5673
C# Type Conversion Methods
Microsoft .NET provides three ways of type conversion:
- Parsing
- Convert Class
- Explicit Cast Operator ()
Parsing
Parsing is used to convert string type data to primitive value type. For this we use parse methods with value types.
int number; number = int.Parse(Console.ReadLine());
Convert Class
string num = "23"; int number = Convert.ToInt32(num);
C# provides the following built-in type conversion methods −
Sr.No. | Methods & Description |
---|---|
1 | ToBoolean
Converts a type to a Boolean value, where possible. |
2 | ToByte
Converts a type to a byte. |
3 | ToChar
Converts a type to a single Unicode character, where possible. |
4 | ToDateTime
Converts a type (integer or string type) to date-time structures. |
5 | ToDecimal
Converts a floating point or integer type to a decimal type. |
6 | ToDouble
Converts a type to a double type. |
7 | ToInt16
Converts a type to a 16-bit integer. |
8 | ToInt32
Converts a type to a 32-bit integer. |
9 | ToInt64
Converts a type to a 64-bit integer. |
10 | ToSbyte
Converts a type to a signed byte type. |
11 | ToSingle
Converts a type to a small floating point number. |
12 | ToString
Converts a type to a string. |
13 | ToType
Converts a type to a specified type. |
14 | ToUInt16
Converts a type to an unsigned int type. |
15 | ToUInt32
Converts a type to an unsigned long type. |
16 | ToUInt64
Converts a type to an unsigned big integer. |
Explicit Cast Operator ()
int num1, num2; float avg; num1 = 10; num2 = 21; avg = (float)(num1 + num2) / 2;
Boxing and unboxing
Boxing and unboxing is an important concept in C# type system. With Boxing and unboxing one can link between value-types and reference-types by allowing any value of a value-type to be converted to and from type object.
Boxing
- Boxing is a mechanism in which value type is converted into reference type.
- It is implicit conversion process in which object type (super type) is used.
- In this process type and value both are stored in object type
Unboxing
- Unboxing is a mechanism in which reference type is converted into value.
- It is explicit conversion process.
Program to show boxing and unboxing:
using System; namespace boxing { class Program { static void Main(string[] args) { int i = 10; object o = i; // boxing int j = (int)o; // unboxing Console.WriteLine("value of o object : " + o); Console.WriteLine("Value of j : " + j); Console.ReadLine(); } } }