C# type conversion


  • Type conversion is a way to convert one data type into another data type.
  • In C# type conversion can be done in two ways:
    • Implicit Type Conversion
    • Explicit Type Conversion

Implicit Type Conversion


  • Implicit type conversion is a way in which one data type is converted into another data type automatically.
  • No data loss occurs in the Implicit Type conversion so it is a type safe conversion.
  • Implicit Type Conversion is basically used where smaller data type is converted into larger data type.
  • Here is an example of Implicit Type Conversion:
Example

using System;
namespace TypeConversion
{
class Implicit
{
static void Main()
{
float x = 4.5f;
double y = x; // Implicit Conversion
Console.WriteLine("x is float ={0}\ny is double ={1}",x,y);
Console.ReadLine();
}
}
}

Output

x is float=4.5
y is double=4.5

Explicit Type Conversion


    a.Explicit Data Type Conversion is a way in which one data type is converted into another data type with the help of some predefined functions. b.Explicit type conversion is done with the help of casting. c.In this type of conversion data loss is occur because the conversion is done forcefully. So it is not a type safe conversion. d.Here is an example of Explicit Type Conversion:
Example

using System;
namespace TypeConversion
{
class Explicit
{
static void Main()
{
//Explicit Conversion
float x = 4.5f;
int y = (int)x;//Explicit type conversion of float to integer.
string s1 = x.ToString();//Explicit type conversion of float to string by ToString Method.
Console.WriteLine("x is float ={0}\ny is integer ={1}\ns1 is string={2}",x,y,s1);
Console.ReadLine();
}
}
}

Output

x is float=4.5
y is integer=4
s1 is string=4.5

In the above example we used ToString method in order to convert float value into string this is a type conversion method. There are various Type Conversion methods present in C#,which are explained in the next section.


Methods of Type Conversion in C#


Following table contains the various type conversion methods which are widely used in C# for the purpose of Type Conversion:


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.