Datatype Conversions in C#

Datatype Conversions in C#

In the previous tutorial, We learnt about nullable types in C# and when to use them and how to use them.

In this tutorial, we are going to learn about implicit conversion, explicit conversion, parse and tryparse methods.

When to use Conversions?

When we want to convert one data type to another data type, we use conversions. Conversions are very important in C#.Some examples of conversions like converting a string into an int etc.

Implicit Conversion

Implicit Conversions are used when we move a smaller datatype to higher datatype. The higher datatype doesn’t throw any error while storing a datatype of smaller value because it has enough memory to store that value. Implicit conversion is done by the compiler itself.
Implicit conversion happens only when
1. When there is no loss of information, when conversion happens
2. If there is no possiblity of throwing exceptions during the conversion.

eg: int myNum =5;
float num=myNum;
In the above example a value is stored in int datatype called myNum. When the int type variable is stored in float type. The compiler doesn’t throw any error because float datatype has enough memory to store the int types.

Explicit Conversion

Explicit Conversion is the reverse of implicit conversion. Explicit Conversion is used when we want to the store the value of a bigger or larger datatype in smaller datatype. Here the loss of information may occur because we are storing bigger data in smaller one. So, the compiler will throw an error indicating that there is loss in data.
So, C# has cast and convert classes which are used when explicit conversion is required.

eg: float myNum=4.567;
int num =myNum; //Compiler throws error
Converting from float to int will lose fractional part. So explicit conversion is required for these types.
float myNum =4.567
int num =(int)myNum
Here, in the above example we are casting float type variable myNum into int datatype by writing int in front of the float variable.
float myNum =4.567
int num =(convert.toint32)myNum;
Here, in the above example we are using convert class in C# to explicitly cast float datatype into int datatype.

Parse and Try Parse Methods

In addition to cast and convert classes, C# also provides parse and Try Parse to convert string data type into another.


Parse method is used to convert string datatype into another datatype.

eg: string num =”100″;
int myNum = int.parse(num);
Here, in the above example the parse method will successfully converts string type into int type.But, there are some cases where it will throw error. One example is

eg: string num=”abc”;
int myNum = int.parse(num);// throws error.

Here, in the above example the compiler cannot convert abc to a numeric value. No compiler in this world can convert a string value like abc to a numeric value. so it throws an error.In these type of situations, c# has another method called tryParse which will help us to get out of the situation.

Try Parse

Try parse method is also used to convert string datatype to another datatype. Unlike parse method, TryParse will return true if succesfully casted or it will return false, if it is not succesfully casted.

eg: string num=”19″;
int Result=0;
bool ret = int.TryParse(num,Result)

Here, in the above example if TryParse successfully converts string to int, ret will have a value of true , if it is unable to convert string to int ret will have a false.

Leave a Reply

Your email address will not be published. Required fields are marked *