Saturday, November 10, 2012

User-Defined Conversions in C#: implicit or explicit

C# allows programmers to declare conversions on classes or structs so that classes or structs can be converted to and/or from other classes or structs, or basic types. Conversions are defined like operators and are named for the type to which they convert.

In C#, conversions can be declared either as implicit, which occur automatically when required, or explicit, which require a cast to be called. All conversions must be static, and must either take the type the conversion is defined on, or return that type.

Example:
 using System;  
   
 namespace ImplicitConversion  
 {  
   class Conversion  
   {  
     static void Main(string[] args)  
     {  
       ConvertibleDouble cdouble;  
   
       // Perform an implicit conversion from a int to a ConvertibleDouble  
       // No cast is required because implicit cast is defined  
       // i.e. public static implicit operator ConvertibleDouble(double val)  
       cdouble = 200;  
       System.Console.WriteLine(cdouble);  
   
       // Perform an implicit conversion from a ConvertibleDouble to a double  
       // No cast is required because implicit cast is defined   
       // i.e. public static implicit operator double(ConvertibleDouble val)  
       double a = new ConvertibleDouble(999);  
       System.Console.WriteLine(a);  
   
       // Perform an explicit conversion from a string to a ConvertibleDoubl  
       // cast is required because explicit cast is defined   
       // i.e. public static explicit operator ConvertibleDouble(string val)  
       string obj = "$400.00";  
       ConvertibleDouble b = (ConvertibleDouble)obj;  
       System.Console.WriteLine(b);  
   
   
       // Perform an explicit conversion from a ConvertibleDoubl to a string  
       // cast is required because explicit cast is defined   
       // i.e. public static explicit operator ConvertibleDouble(string val)       
       string d = (string)b;  
       System.Console.WriteLine(d);  
   
   
       System.Console.WriteLine("Press any key to exit.");  
       System.Console.ReadKey();  
     }  
   }  
   
   struct ConvertibleDouble  
   {  
     private double _val;  
   
     public ConvertibleDouble(double val)  
     {  
       this._val = val;  
     }  
   
     public static implicit operator ConvertibleDouble(double val)  
     {  
       return new ConvertibleDouble(val);  
     }  
   
     public static implicit operator double(ConvertibleDouble val)  
     {  
       return val._val;  
     }  
   
     public static explicit operator ConvertibleDouble(string val)  
     {  
       return new ConvertibleDouble(Convert.ToDouble(val.Trim().Replace("$", "")));  
     }  
   
     public static explicit operator string(ConvertibleDouble val)  
     {  
       return string.Format("${0:N2}", val._val);  
     }  
   
     public override string ToString()  
     {  
       return _val.ToString();  
     }  
   }  
 }  
   


OutPut:
 200  
 999  
 400  
 $400.00  
 Press any key to exit.  
   

No comments:

Post a Comment

There was an error in this gadget