find and solve ||
Please wait.....

How to convert data from one type to another in C#?

 Data type conversion refers to the process of converting the values of a data type into another data type. When the value of a variable of one data type is converted into the value of another data type, only the data type of the value is changed; the value itself remains the same.In c# the following types of conversions are available:

  • Implicit Conversion

  • Explicit Conversion 

Let's discuss both these conversion types, one by one.

Implicit Conversion

As the name suggests, implicit conversion refers to the process in which the c# compiler simplicity or automatically converts the value of a data type to the value of another data type without any loss of data.

Given table lists the implicit conversions allowed for different data types:

Data TypePossible conversion To

short, int,long, float, double,decimal

byteshort,short, int, uint, long,long, float, double,decimal

int, long,float, double,decimal

ushortint, uint, long,ulong, float,double,decimal

long, float, double,decimal


long, float, double,decimal

longfloat, double,decimal

float, double,decimal



charushort, int, uint, long,ulong, float, double, decimal
The following code snippet shows an example of implicit conversion:
short  a =21;
int  b =a; //implicit conversion
The first line of the preceding code snippet declares a variable, a of short data type, and initializes it with value,21. The second line declares a variable,b, of int data type, and initializes it with the value stored in a. The second line involves implicit conversion as conversion from a data type (short)to another data type (int)is done without explicitly specifying the target data type. Note that the text after the double forward slash characters (//) is a comment, which is not executed by the compiler.

Explicit Conversion

As you have seen that all data types could not be implicitly converted to others. For example, you cannot implicitly convert a string type value to an int type. In such situations, you need to explicitly request the C# compiler to perform the conversion. You can perform an explicit conversion through casting, which refers to the process of converting data types by prefixing a value or variable with the intended data type 

The syntax for casting data types is as follows:
variable2=(Data_Type) variable1;
In the preceding syntax,
#Variable1: Refers to the variable whose value you want to convert
#Data_Type :Refers to the data type to which you want to convert the value of Variable1
#Variable2: Refers to the variable that holds the converted value
The following code snippet shows an example of casting of data types:

float A=24.0F;
int B= (int) A; //B holds the value 24
In the preceding code snippet, the float variable, A, has the value, of 24.05. The value of A is converted into an int type by prefixing A with (int) while initializing the B variable. As a result of this explicit conversion, the fractional part of the value stored in the A variable is truncated and the integral part (24) of the value is assigned to the B variable. In this way, you can see that explicit conversions may involve data loss.
While developing programs in C#, several times you need to perform conversions between int and string data types. The ToString() AND Parse() methods are two predefined methods that allow you to convert an int type into a string type and vice-versa.

In C#, the Tostring() method allows you to convert int data type (and other data types) to its string equivalent. The following code snippet shows how to use the ToString() method to assign the value stored in the int variable, Age,to a variable,strAge, of the string type:

int age =27;
string strAge= Age.ToString();//strAge is equal to "27"
To convert the string type to the int type. you can use the Parse() method. In the flowing code snippet, the value of the string type variable,Phone is converted to int type by using the Parse() method:

string phone ="984754121";
int phoneNumber = int.Parse(phone);//phoneNumber is equal to 984754121
The following example shows an explicit type of conversion
using System;
namespace ExplicitExample{
   class ExplicitConversion {
      static void Main(string[] args) {
         double d = 2345.74; 
         int i;
         // cast double to int.
         i = (int)d;


The following example converts various value types to string type 
using System;
namespace ExplicitExample{
   class StringConversion {
      static void Main(string[] args) {
         int i = 65;
         float f = 51.005f;
         double d = 235.7152;
         bool b = true;

Related information

Sundar  Neupane

Sundar Neupane

I like working on projects with a team that cares about creating beautiful and usable interfaces.

If felt valuable to you, feel free to share it.


Report Response