Data Type Conversion In C#

DataType conversion in C# is classified into 2 types.
  • Implicit Conversion
  • Explicit Conversion

Implicit Conversion

Implicit Conversion is automatically done by the compiler. For implicit conversion, we don't have to do anything manually. During implicit conversion, there is no loss of information and there is no possibility of throwing any overflow exception when the conversion is done.

Example

  1. class Program {  
  2.     static void Main(string[] args) {  
  3.         int i = 100;  
  4.         float f = i; // Implicit Conversion.  
  5.         Console.WriteLine("Converted float value is :{0}", f);  
  6.     }  
  7. }  

In the above line of code, an integer is implicitly converted into float. There won't be any loss of data because float is much bigger datatype compared to an integer and also, it won't throw any overflow exception.

Explicit Conversion

Explicit conversion is the conversion where we need to manually convert from one datatype to another datatype. Compiler won't do the conversion for us.

For Example, when we convert a float datatype to int datatype, we lose the fractional part (loss of information) and also, there is a possibility of throwing an overflow exception.

This is the main reason compiler won't do the conversion for us and we are forced to do the conversion explicitly.

There are 2 ways we can explicitly convert one datatype to another datatype.

  • Type Cast operator
  • Convert class

Type Cast Operator - (int)

Type Cast operator can be used for explicitly converting one datatype to another datatype. When we use a Type Cast operator for converting one datatype to another datatype we won't get the exception if the number which is used for conversion is much bigger than the expected range. It will only give a random negative value (-23452344).

Convert Class - Convert.ToInt32()

Convert Class also can be used for explicitly converting one datatype to another datatype. When we use a Convert class for converting one datatype to another datatype, compiler will generate an unhandled exception if the value which is used for converting is outside the expected range.

Example

  1. class Program {  
  2.     static void Main(string[] args) {  
  3.         float d = 12343.45 F;  
  4.         int j = (int) d; //Explicit Conversion - Type Cast operator  
  5.         int s = Convert.ToInt32(d); //Explicit Conversion - Convert Class  
  6.         Console.WriteLine("Converted int value using the Type cast operator is : {0}", j);  
  7.         Console.WriteLine("Converted int value using the convert class is : {0}", s);  
  8.     }  
  9. }  

If a number is in String format, we have 2 options for converting it into integer format

  • Parse
  • Try Parse

Parse Method

Parse method can be used for converting a number in string format to an integer format. When we use a parse method compiler throws an exception if it cannot parse the value. For example if the number contains any string value then it will be an invalid conversion when converting it into an integer format so compiler throws an unhandled exception. The return value of the parse method is int. We can use the parse method only under the situation where we are 100 % sure that we are converting an integer which is in string format.

Try Parse Method

Try Parse method can also be used for converting a number in string format to an integer format. The return type of the Try Parse method is a Boolean value. Compiler won't throw an exception if it cannot parse the value. It tries to parse the value - if it succeeds, then it returns a True value otherwise a False Value. This is one of main differences between Parse and Try Parse method.

Example

  1. class Program {  
  2.     static void Main(string[] args) {  
  3.         string strValue = "100";  
  4.         int intValue = int.Parse(strValue);  
  5.         Console.WriteLine("Converted int value using parse method is {0}", intValue);  
  6.         string strVal = "100";  
  7.         int intVal = 0;  
  8.         bool isConvert = int.TryParse(strVal, out intVal);  
  9.         Console.WriteLine("Converted int value using try parse method is : {0} and its status is : {1}", intVal, isConvert);  
  10.         Console.ReadLine();  
  11.     }  
  12. }  

The free online CSS code beautifier takes care of your dirty code and strips every unwanted mess. Click here to get started.

Next Recommended Reading Data Types & Type Conversions in C#