C# – implicit & explicit keywords

Posted on Updated on

What are the uses of the implicit and explicit keywords in C#.

implicit is used when we need implicit conversions between types. It works on value types and as well on reference types.

The below code shows the use of implicit keyword. Create a class named Digit as below and note the two static methods with implicit keyword.

   1: class Digit

   2: {

   3:     public double val;

   4:     

   5:     public Digit(double d)

   6:     {

   7:         val = d;

   8:     }

   9:  

  10:     public static implicit operator double(Digit d)

  11:     {

  12:         return d.val;

  13:     }

  14:  

  15:     public static implicit operator Digit(double d)

  16:     {

  17:         return new Digit(d);

  18:     }

  19:  

  20: }

Since the Digit class has both the Digit to double conversion and the double to Digit conversion implicit functions defined, it is safe to do the following.

   1: Digit dig = new Digit(7);

   2:  

   3: //This call invokes the implicit "double" operator 

   4: double num = dig;

   5: //This call invokes the implicit "Digit" operator

   6: Digit dig2 = 12;

Same works on complete reference types as well. Look at the following example.

   1: class Animal

   2: {

   3:     public string  Family { get; set; }

   4:  

   5:     public static implicit operator Dog(Animal animal)

   6:     {

   7:         return new Dog() { Breed = animal.Family };

   8:     }

   9:  

  10:     public static implicit operator Animal(Dog d)

  11:     {

  12:         return new Animal() { Family = d.Breed };

  13:     }

  14: }

  15:  

  16: class Dog

  17: {

  18:     public string Breed { get; set; }

  19: }

These following conversions are possible.

   1: Animal a = new Animal() { Family = "Golden Retriever" };

   2: Dog d1 = a;

   3: a = d1;

 

explicit keyword does the right opposite, where we have to explicitly cast the one type to the other. Say we’ve a set of classed like this.

   1: class WaterCleaner

   2: {

   3:     public string Name;

   4: }

   5:  

   6: class RoboticCleaner

   7: {

   8:     public string Name;

   9:  

  10:     public static explicit operator WaterCleaner(RoboticCleaner r)

  11:     {

  12:         return new WaterCleaner() { Name = r.Name };

  13:     }

  14:  

  15:     public static explicit operator RoboticCleaner(WaterCleaner w)

  16:     {

  17:         return new RoboticCleaner() { Name = w.Name };

  18:     }

  19: }

Then you should do the explicit casting like this.

   1: RoboticCleaner rc = new RoboticCleaner() { Name = "Super Cleaner" };

   2: WaterCleaner wc = (WaterCleaner)rc;

 

If RoboticCleaner has implicit conversion defined then you don’t need the explicit conversion.

Now the question is what if the RoboticCleaner class has both implicit and explicit conversions defined.

Now your RoboticCleaner should be like this.

   1: class RoboticCleaner

   2: {

   3:     public string Name;

   4:  

   5:     public static explicit operator WaterCleaner(RoboticCleaner r)

   6:     {

   7:         return new WaterCleaner() { Name = r.Name };

   8:     }

   9:  

  10:     public static explicit operator RoboticCleaner(WaterCleaner w)

  11:     {

  12:         return new RoboticCleaner() { Name = w.Name };

  13:     }

  14:  

  15:     public static implicit operator WaterCleaner(RoboticCleaner r)

  16:     {

  17:         return new WaterCleaner() { Name = r.Name };

  18:     }

  19: }

Then the compiler gets confused and shows this message. (click on the image to enlarge)

image

Advertisements