One of the little used features of c#, is user written implicit and explicit type conversion. Put simply: sometimes you have a object (custom or built-in .Net framework) and want to convert it to another type of object. While you can easily write left to right style code to accomplish this every time, there is a lot cleaner way of doing this by empowering your objects to cast themselves as another type.
Square peg round hole
Conversion of a type is as mentioned above when you want to convert between two different types of objects. There are usually two types of conversion referred to and they have been usually referred to as widening conversions and narrowing conversions.
Widening conversions
When an object type is converted to another type that is capable of storing additional data to the source object type this is referred to as widening conversion. The easiest examples to understand are ones between say Byte, Integer, Decimal, Double etc. I have listed them in this order to demonstrate that each type is followed by a type that stores more data. This type of conversion is made easier because it will never cause overflow.
Narrowing conversions
This is the type of conversion that you do when you are trying to convert to an object type that contains less information. These are known to lose information, such as converting from a double an integer.
Why do i care? - Custom conversions
Well the cool thing is you can add code to your classes to make them convert between each other, making for great portability.
if you have two classes MyType and MyOtherType that have completely different properties and try to write the following code it will fail to compile (obviously):
MyType obj1 = new MyType(); MyOtherType obj2 = obj1;
Additionally, so will this:
MyOtherType obj1 = new MyOtherType (); MyType obj2 = obj1;
While if you have control over the creation of both these classes you can easily take care of these issues using interfaces, making both classes both comply to a shared contract, where this is a really an issue is when you don’t and are dealing with a foreign object data type.
As there are lots of different scenarios where you would want to convert between your classes and another, i will show you how to allow to empower your class to convert to another class and additionally convert from another class in a very easy and transparent way.
Converting from (Explicit conversion)
You can use this method of conversion using explicit casting.
class MyType { public static explicit operator MyType(MyOtherType mo) { // code to convert from MyOtherType to MyType // and return a MyType object ie. MyType m = new MyType(); m.MyProperty = mo.MyOtherTypeProperty; return m; } }
Usage:
MyType myType = (MyType)myOtherTypeObject;
Converting to (Implicit conversion)
This method requires no explicit casting, as the conversion is implied.
class MyType { public static implicit operator MyOtherType(MyType typ) { // code to convert from MyType to MyOtherType // and return a MyOtherType object ie. MyOtherType m = new MyOtherType(); m.MyOtherTypeProperty = typ.MyTypeProperty; return m; } }
Usage:
MyOtherType myOtherType = myTypeObject;