Monday, 12 January 2015

Datatype Conversion

1. Implicit conversion and explicit conversion


a) Implicit Conversion:

Implicit conversion is done by the compiler.
1). When there is no loss of information and the size of target type is larger than the size of previous type.
2).If there is no possibilities of throwing the exception during the conversion

Example: Converting an type int to float type will not loose any data , no exception will be thrown hence an implicit conversion can be done.

Size of int is -2,147,483,648 to 2,147,483,647 and size of float is -3.4 × 1038 to +3.4 × 1038

Exp:


  1. using System;  
  2. class implicitDemo  
  3. {  
  4.    public static void Main()  
  5.    {  
  6.        int i = 100;  
  7.        float j=i;  
  8.       Console.WriteLine(j);  
  9.    }  
  10. }  

Implicit conversion output screenshots
Fig: Screenshots of Output

b). Explicit Conversion :


When converting a float type to int type, we loose fractional part and also possibilities of overflow exception. Hence, in this case an Explicit conversion is reqiured. For explicit conversion we can use cast operator or the convert class

Example:

1.
  1. using System;  
  2. class ExplicitDemo  
  3. {  
  4.    public static void Main()  
  5.   {  
  6.       float f = 10987452135547895640.256F;  
  7.       float fi = 2558679458632566.1296F; 
  8.   
  9.     int i = f;    
  10.     int j = fi; 
  11.   
  12.     Console.WriteLine("{0}{1}", i,  j);  
  13.   }  
  14.   
  15. }  

  On compiling the above code it gives error:
Fig: Screenshots of Output

2. Modified code

  1. using System;  
  2. class ExplicitDemo  
  3. {  
  4.    public static void Main()  
  5.   {  
  6.       float f = 100.256F;  
  7.       float fi = 256.1296F  
  8.   
  9.     int i = (int)f;    // Type cast Operator  
  10.     int j = Convert.ToInt32(fi);   //using convert class  
  11.   
  12.     Console.WriteLine("{0}{1}", i,  j);  
  13.   }  
  14.   
  15. }  

Output of explicit conversion
Fig: Screenshots of Output


2. Parse() and TryParse()


If the number is in string format you have two option to convert them into another type one is Parse() and other is TryParse().

Parse() method throws an exception if it cannot parse the value, whereas TryParse() returns a bool indicating weather it succeeded or failed.

Use Parse() if you sure the value will be valid otherwise use TryParse().


Example 1. simple parse program

  1. using System;  
  2. class ParseDemo  
  3. {  
  4.     public static void Main()  
  5.     {  
  6.         string strNumber = "100";  
  7.   
  8.         int i = int.Parse(strNumber);  
  9.   
  10.         Console.WriteLine(i);  
  11.     }  

Parse() method output
Fig: Output of Parse() method

Example 2. Parse() method throw an Exception if it cannot parse the value

  1. using System;  
  2. class ParseDemo  
  3. {  
  4.     public static void Main()  
  5.     {  
  6.         string strNumber = "100Sa";  
  7.   
  8.         int i = int.Parse(strNumber);  
  9.   
  10.         Console.WriteLine(i);  
  11.     }  
  12. }  

Unhandled Exception of Parse method
Fig: Unhandled Exception


Example 3. TryParse() returns bool in case of failure

  1. using System;  
  2. class ParseDemo  
  3. {  
  4.     public static void Main()  
  5.     {  
  6.         string strNumber = "100San";  
  7.   
  8.         int Result = 0;  
  9.   
  10.         bool IsConversionSucessful = int.TryParse(strNumber, out Result);  
  11.   
  12.         if(IsConversionSucessful)  
  13.         {  
  14.             Console.WriteLine(Result);  
  15.         }  
  16.   
  17.         else  
  18.         {  
  19.             Console.WriteLine("Please enter a valid Number");  
  20.         }  
  21.     }  
  22. }  

Fig: Output of TryParse()