Overloading Conversion Operators

You can overload the conversion behaviors in C#. For example, what if you want to convert one class into an unrelated class. We can overload the effect of casting or the implicit conversion. The example below shows a program that demonstrates how we can overload the conversion operators.

using System;
 
namespace OverloadingConversionsOperatorsDemo
{
    class Animal
    {
        public int Height { get; set; }
 
        public static implicit operator Plant(Animal animal)
        {
            Plant result = new Plant();
            result.Height = animal.Height;
            return result;
        }
    }
 
    class Plant
    {
        public int Height { get; set; }
 
        public static explicit operator Animal(Plant plant)
        {
            Animal result = new Animal();
            result.Height = plant.Height;
            return result;
        }
    }
 
    class Program
    {
        public static void Main()
        {
            Animal myAnimal = new Animal();
            myAnimal.Height = 100;
 
            //Implicit conversion 
            Plant myPlant = myAnimal;
 
            Console.WriteLine("myAnimal.Height = {0}cm", myAnimal.Height);
            Console.WriteLine("myPlant.Height = {0}cm", myPlant.Height);
 
            myPlant.Height = 200;
 
            //Explicit conversion
            myAnimal = (Animal)myPlant;
 
            Console.WriteLine("\nmyAnimal.Height = {0}cm", myAnimal.Height);
            Console.WriteLine("myPlant.Height = {0}cm", myPlant.Height);
        }
    }
}

Example 1 – Overloading Conversion Operators

myAnimal.Height = 100cm
myPlant.Height = 100cm

myAnimal.Height = 200cm
myPlant.Height = 200cm

You can see that the two classes, Animal and Plant, doesn’t inherit each other, therefore, they are not related(at least they are through System.Object). We defined the overloading of the conversion for each class. The Animal class overloads its implicit conversion to the Plant class. Notice that we used the implicit keyword to indicate the we are overloading the implicit conversion, and the operatorkeyword to indicate that the method will be used for overloading. We write the name of the destination class. When we assign the Animal object to a Plant object, the code inside the overloading method will be executed. The method requires one parameter which is the a reference to the object being converted. There is no return type or method name, and the method should be static and public. Inside the method, we created a new instance of the Animal class and assigned the value of the Height property of the passed Plantobject to the Height property of the created Animal class. Basically, you should put all the code for transferring the necessary values from the source object to the destination object.

On the second method, we used the explicit keyword instead of the implicit to signify that we are overloading the explicitconversion for that class. The syntax is pretty much the same except for the explicit keyword. We used the same behavior inside the overloading method.

When we watch those overloaded conversions in actions, you can see that the values of Height was indeed copied to the other class. Notice the implicit conversion of the Animal class to a Plant object.

Plant myPlant = myAnimal;

We cannot invert the position of them like this:

Animal myAnimal = myPlant;

This is because no overloading method was defined for the explicit conversion in the Animal class. But we know that the Plant class has one, so the following code uses that overloaded explicit conversion.

Animal myAnimal = (Animal)myPlant;

We used casting to do an explicit conversion.

Written by compitionpoint

Leave a Comment

Your email address will not be published. Required fields are marked *