Introduction
Did you know that an interface binds your class to implement the defined functionalities declared within it? I have to mention this because we are going to explore how to play with IConvertible interface. In this post, we will try to implement the IConvertible using a simple example. Let’s get started.
What is IConvertible Interface?
The IConvertible interface basically provides methods that developers can use for the conversion of different types. Once you have decided to implement this interface, you must choose the field/fields within your type to support the conversion.
The conversion takes place when you have decided to invoke the implemented methods of this interface. In addition, the behavior of this conversion converts the value(s) of the implemented type to a common language runtime type.
The common language runtime types are the following: Boolean, SByte, Byte, Int16, UInt16, UInt32, Int64, UInt64, Single, Double, Decimal, DateTime, Char, and String.
Enough with the definitions, let's have an example.
- public partial class Car {
- public string Brand { get; set; }
- public string Variant { get; set; }
- public bool BestSeller { get; set; }
- public DateTime DateBuilt { get; set; }
- public double SurfaceArea { get; set; }
- public double Length { get; set; }
- public double Width { get; set; }
- public double Height { get; set; }
- }
- public partial class Car : IConvertible {
- public TypeCode GetTypeCode () {
- return TypeCode.Object;
- }
- public bool ToBoolean (IFormatProvider provider) {
- return this.BestSeller;
- }
- public byte ToByte (IFormatProvider provider) {
- return (byte)this.SurfaceArea;
- }
- public char ToChar (IFormatProvider provider) {
- return this.Brand[0];
- }
- public DateTime ToDateTime (IFormatProvider provider) {
- return this.DateBuilt;
- }
- public decimal ToDecimal (IFormatProvider provider) {
- return (decimal) this.SurfaceArea;
- }
- public double ToDouble (IFormatProvider provider) {
- return this.SurfaceArea;
- }
- public short ToInt16 (IFormatProvider provider) {
- return (short) this.SurfaceArea;
- }
- public int ToInt32 (IFormatProvider provider) {
- return (int) this.SurfaceArea;
- }
- public long ToInt64 (IFormatProvider provider) {
- return (long) this.SurfaceArea;
- }
- public sbyte ToSByte (IFormatProvider provider) {
- return Convert.ToSByte (this.SurfaceArea);
- }
- public float ToSingle (IFormatProvider provider) {
- return (float) this.SurfaceArea;
- }
- public string ToString (IFormatProvider provider) {
- return $"Brand: {this.Brand} Variant: {this.Variant}";
- }
- public object ToType (Type conversionType, IFormatProvider provider) {
- return Convert.ChangeType (this.SurfaceArea, conversionType);
- }
- public ushort ToUInt16 (IFormatProvider provider) {
- return (ushort) this.SurfaceArea;
- }
- public uint ToUInt32 (IFormatProvider provider) {
- return (uint) this.SurfaceArea;
- }
- public ulong ToUInt64 (IFormatProvider provider) {
- return (ulong) this.SurfaceArea;
- }
- }


Sourabh SomaniPosted Nov 10, 2019, 10:55 PM
Awesome blog. :)