Type conversion example in C# .NET using the IConvertible interface

In this we saw how to convert between numeric types explicitly and implicitly. There are other ways to implement conversions in C#. You must have come across the System.Convert static methods such as System.ConvertToInt32 or System.ConvertToByte.

You can implement your own conversions by implementing the IConvertible interface. Consider the following object:

The solution is to implement the IConvertible interface. Here comes a warning: this interface comes with 17 methods such as ToDecimal or ToUInt16. You don’t need to implement all of them as not everything will make sense for your object. E.g. it’s probably meaningless to convert the above House object to a byte or char. Here are the methods that I’ve implemented. I left the rest with the default throw new NotImplementedException(); body: