Custom Implicit and Explicit type conversions in C#

csharpWe all know what type casting is. It’s converting from one type to the other. But what exactly is implicit type casting?

Well implicit type casting is a feature of C# that helps you convert instances of custom Classes you created from one to another.


Now let’s say that  we have a class named Root and a Class named Jailbreak. For simplicity of our example both classes only contain one field, a boolean value named fullAccess.
First let’s take a look at our classes shall we?


class Root
{
     public bool fullAccess;
}

class Jailbreak
{
     internal Jailbreak(bool fullAccess)
     {
         FullAccess = fullAccess;
     }

     public bool FullAccess { get; }
}

 
So you can see they have on slight difference. Root class has only a public boolean field while Jailbreak class has a public get only property and a constructor to set that property.
How can we use implicit conversion to help us simplify the process of converting from one to another?
Well first, we need to define in our classes how they should behave in these situations.

class Root
{
     public bool fullAccess;
     public static implicit operator Jailbreak(Root r)
     {
        Jailbreak j = new Jailbreak(r.fullAccess);
        return j;
     }
}

class Jailbreak
{
     internal Jailbreak(bool fullAccess)
     {
         FullAccess = fullAccess;
     }

     public bool FullAccess { get; }
     public static implicit operator Root(Jailbreak j)
     {
        Root r = new Root();
        r.fullAccess = j.FullAccess;
        return r;
     }
}

And now to make use of implicit type conversion all we have to do is the following.

class Program
{
    static void Main()
    {
       Root r = new Root();
       r.fullAccess = true;
       Console.WriteLine(r.fullAccess);

       // Implicit conversion from root to jailbreak.
       Jailbreak j = r;
       Console.WriteLine(j.FullAccess);

       // Implicit conversion from jailbreak to root.
       Root r2 = j;
       Console.WriteLine(r2.fullAccess);
    }
}

And that’s all Folks!! Nothing more to do here. When implementing the implicit type conversion methods within the class, the rest is automated.

Now for explicit conversions. 
We can implement explicit conversions from our custom object to anything else just like with implicit conversions. The only difference is we have to type explicit and not implicit when we are defining it like so

class Root
{
     public bool fullAccess;
     public static explicit operator Jailbreak(Root r)
     {
        Jailbreak j = new Jailbreak();
        j.fullAccess = r.fullAccess;
        return j;
     }
}

and to perform the conversion we simply do

class Program
{
    static void Main()
    {
       Root r = new Root();
       r.fullAccess = true;
       Console.WriteLine(r.fullAccess);

       // Implicit conversion from root to jailbreak.
       Jailbreak j = (Jailbreak)r;
       Console.WriteLine(j.fullAccess);
    }
}

That’s all there is to it.

 

Just a final note, you can always have more than one implicit or explicit type conversions in a single class. You do not need to have only one. As long as you do not have more than one for the same type you are converting to.

 

Please follow and like us:

Leave a Reply