Saturday 6 July 2013

Implicit and Explicit Conversion in C#

In C# Language , There is concept called Type Conversions, Which will convert one type to another.

In that there are two types of Conversions
1. Implicit Conversion
2. Explicit Conversion

Implicit Conversion : 
Conversion between types can be achieved automatically is known as Implicit
 For Ex :  

        byte value1 = 10;
        byte value2 = 23;
        long  total    = value1 + value2;

Here in this example, we don't need to specify data type for the target type, internally it will convert that is implicit. value1 , value2 are byte when adding we assign the value to a variable total which is long data type.
Internally it converts the result value into long data type.

Explicit Conversion :
Conversion between types can be done Manually instead of automatically
For Ex:
        
        long val = 3000;
        int a = (int) val;       /*  valid cast . The Maximum int is 2147483647  */

In this above example, we can see that val is long data type now it is assign to int type with explicitly specify 
to which data type we have to convert . If we misses this explicit convert C# Compiler throws error.

Let we see in coding How it is works like this Implicit and Explicit conversion 

Here we take three class  Rupees Dollar  ,Euro     

Rupees can be convert to Euro or Dollar   Implicit
Dollar   can be convert to Rupees Implicit and Euro as Explicit
Euro     can be convert to Rupees or Dollar   Implicit

From the below code 20 is pass as a parameter to rupees, then it is implicitly converted to Dollar and 
Euro

/***********************************************************************/

     Rupees rup  =  new Rupees(20);
     Dollar   rdrl  =  rup;
     Euro     reur  =  rup;

   class Rupees     {
        public decimal Value{set;get;}
    
    
        public Rupees(decimal value)
        {
            this.Value = value;
        }
    
       /* Rupees can be implicitly converted to Euro */
        public static implicit operator Euro(Rupees cur)
        {        
            return new Euro(cur.Value/100 );
        }
         
        /* Rupees can be implicitly converted to Dollar */
        public static implicit operator Dollar(Rupees cur)
        {
            return new Dollar(cur.Value/50);
        }
    }
/***********************************************************************/


/***********************************************************************/
Here Dollar is convert to Euro Explicitly 

     Dollar    dlr    =  new Dollar(4);
     Euro      deur =  (Euro)dlr;
     Rupees drup =  dlr;

class Dollar
    {
        public decimal Value{set;get;}
    
    
        public Dollar(decimal value)
        {
            this.Value = value;
        }
    
        /* Dollar can be implicitly converted to Rupees */
        public static implicit operator Rupees(Dollar cur)
        {        
            return new Rupees(cur.Value*50);
        }
            
        /* Dollar can be implicitly converted to Euro */
        public static explicit operator Euro(Dollar cur)
        {
            return new Euro(cur.Value/2);
        }
    }
 /***********************************************************************/   

/***********************************************************************/
In the below code Euro is implicitly convert to rupees and Explicitly to Dollar

     Euro      eur   =   new Euro(2);
     Rupees erup =   eur;
     Dollar   edlr  =   (Dollar)eur;

class Euro
    {
        public decimal Value{set;get;}
    
        public Euro(decimal value)
        {
            this.Value = value;
        }
    
        /* Euro can be implicitly converted to Rupees */
        public static implicit operator Rupees(Euro cur)
        {        
            return new Rupees(cur.Value*100);
        }

      /* Euro can be implicitly converted to Dollar */
        public static implicit operator Dollar(Euro cur)
        {
            return new Dollar(cur.Value*2);
        }
    }
 /***********************************************************************/
public static void Main(string[] args)
{
            Rupees r     = new Rupees(20);
            Dollar   rdrl = r;
            Euro     reur = r;
            Console.WriteLine("Rupees = {0} \tDollar = {1} \tEuro = {2}",r.Value,rdrl.Value,reur.Value);
        
            Dollar  dlr    =   new Dollar(4);
            Euro    deur  =   (Euro)dlr;
            Rupees drup=   dlr;
            Console.WriteLine("Dollar = {0} \tEuro = {1} \tRupees = {2}",dlr.Value,deur.Value,drup.Value);
        
            Euro      eur   = new Euro(2);
            Rupees erup = eur;
            Dollar    edlr = (Dollar )eur;
            Console.WriteLine("Euro = {0} \tRupees = {1} \tDollar = {2}",eur.Value,erup.Value,edlr.Value);
        
            Console.WriteLine();
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
  }

Following is the ouput :                 





The full source code for Type Conversion  Code.

namespace TypeConversion
{
    class Dollar
    {
        public decimal Value{set;get;}
    
    
        public Dollar(decimal value)
        {
            this.Value = value;
        }
    
    
        public static implicit operator Rupees(Dollar cur)
        {        
            return new Rupees(cur.Value*50);
        }
        
        public static explicit operator Euro(Dollar cur)
        {
            return new Euro(cur.Value/2);
        }
    }

    class Euro
    {
        public decimal Value{set;get;}
    
        public Euro(decimal value)
        {
            this.Value = value;
        }
    
        public static implicit operator Rupees(Euro cur)
        {        
            return new Rupees(cur.Value*100);
        }
        
        public static implicit operator Dollar(Euro cur)
        {
            return new Dollar(cur.Value*2);
        }
    }

    class Rupees
    {
        public decimal Value{set;get;}
    
    
        public Rupees(decimal value)
        {
            this.Value = value;
        }
    
        public static implicit operator Euro(Rupees cur)
        {        
            return new Euro(cur.Value/100 );
        }
        
        public static implicit operator Dollar(Rupees cur)
        {
            return new Dollar(cur.Value/50);
        }
    }

    class Program
    {
        public static void Main(string[] args)
        {
            Rupees r=new Rupees(20);
            Dollar rdrl=r;
            Euro reur=r;
            Console.WriteLine("Rupees = {0} \tDollar = {1} \tEuro = {2}",r.Value,rdrl.Value,reur.Value);
            
            Dollar dlr =new Dollar(4);
            Euro deur=(Euro)dlr;
            Rupees drup=dlr;
            Console.WriteLine("Dollar = {0} \tEuro = {1} \tRupees = {2}",dlr.Value,deur.Value,drup.Value);
            
            Euro eur=new Euro(2);
            Rupees erup=eur;
            Dollar edlr=(Dollar)eur;
            Console.WriteLine("Euro = {0} \tRupees = {1} \tDollar = {2}",eur.Value,erup.Value,edlr.Value);
            
             Console.WriteLine();
             Console.Write("Press any key to continue . . . ");
             Console.ReadKey(true);
        }
    }
}


This article will make a Clear understanding of Implicit and Explicit type conversion.














1 comment:

  1. It's superior, however , check out material at the street address. conversion money

    ReplyDelete