C# allows for implicit and explicit type conversion between classes when there is an inheritance situation. Something like this:
class BaseClass { }
class DerivedFromBaseClass : BaseClass { }
class Program
{
static void Main(string[] args)
{
BaseClass bc = new DerivedFromBaseClass(); //implicit conversion
DerivedFromBaseClass dbc = (DerivedFromBaseClass)bc; //explicit conversion
}
}
Ok, this shouldn’t be anything new to know since this is a feature present in all object oriented languages I know. But now I want to talk about a stranger kind of conversion and for this I’ll have to mix apples and oranges.
Let’s define to classes: Apple and Orange.
class Apple
{
public string Farm { get; set; }
public Apple(string farm)
{
this.Farm = farm;
}
public override string ToString()
{
return string.Format("I'm an apple from the {0} farm", this.Farm);
}
}
class Orange
{
public string Farm { get; set; }
public Orange(string farm)
{
this.Farm = farm;
}
public override string ToString()
{
return string.Format("I'm an orange from the {0} farm", this.Farm);
}
}
As you can see these two classes are totally unrelated, they don’t have any inheritance relation with each other.
Let’s convert Apples to Oranges using the cast-like sintax. This is possible using the keywords ’’‘operator’’’, ’’‘explicit’’’ in a static method that will do the conversion. Here are the changes to the Apple class so that it can be converted explicitly to an Orange.
class Apple
{
public string Farm { get; set; }
public Apple(string farm)
{
this.Farm = farm;
}
public override string ToString()
{
return string.Format("I'm an apple from the {0} farm", this.Farm);
}
public static explicit operator Apple(Orange o)
{
Apple a = new Apple(o.Farm);
return a;
}
}
class Program
{
static void Main(string[] args)
{
Orange o = new Orange("GreatSouth");
Console.WriteLine(o.ToString());
Apple a = (Apple)o; //explicit conversion
Console.WriteLine(a.ToString());
}
}
Ok, it works. You can also do an implicit conversion but in this case you must be aware that it will also create (automatically) the explicit conversion. This is why you can’t define both these conversions in the same class. Following is the changes necessary to the Orange class so that you can make implicit and explicit conversions from Apples to Oranges:
class Orange
{
public string Farm { get; set; }
public Orange(string farm)
{
this.Farm = farm;
}
public override string ToString()
{
return string.Format("I'm an orange from the {0} farm", this.Farm);
}
//this will allow implicit and explicit conversion
public static implicit operator Orange(Apple a)
{
Orange o = new Orange(a.Farm);
return o;
}
}
class Program
{
static void Main(string[] args)
{
Apple a = new Apple("GreatSouth");
Console.WriteLine(a.ToString());
Orange o = a; //implicit
Console.WriteLine(o.ToString());
//or you could do it like this
o = (Orange)a; //explicit
Console.WriteLine(o.ToString());
}
}
I hope you found this interesting. Once again a know that my examples are not always the most thought out but I’m certain that you got the idea and will be able to put it work in a real life situation.