CopyPastehas never been so tasty!

Using IConvertible Interface for data types conversion in C#

by anonymous

  • 0
  • 0
  • 0
114 views

Orignal Source:http://codingvilla.com/using-iconvertible-interface-csharp-single-article-601.aspx

 

Interface:

 

In c# interface provide runtime polymorphism. It provides multiple inheritance.  Interface has only signature of the function and the implementation of methods are done in that class which implements the interface.

 

IConvertible Interface:

 

It is an interface which has a large number of methods such as ToDecimal, ToDouble etc. It is used for data type conversions. It converts the value of that reference of value type which implements it and then converts it into CLR type.

 

Description:

 

Here we have an example in which we convert the one data type into another data type. The class implements the IConvertible interface. We define two variables and initialize them into the constructor. In main we create an object of class and pass that object as parameter to the method of convert class in which we want to convert it.

 

To demonstrate make a console application and write the following code.

 

Code:

 

classComplex : IConvertible

  {

double  number1;

double  number2;

 

public Complex(double number1, double number2)

    {

this.number1 = number1;

this.number2 = number2;

    }

 

publicTypeCodeGetTypeCode()

    {

returnTypeCode.Object;

    }

 

boolIConvertible.ToBoolean(IFormatProvider provider)

    {

if(  (number1 != 0.0) || (number2 != 0.0) )

returntrue;

else

returnfalse;

    }

 

doubleGetDoubleValue()

    {

returnMath.Sqrt(number1*number1 + number2*number2);

    }

 

byteIConvertible.ToByte(IFormatProvider provider)

    {

returnConvert.ToByte(GetDoubleValue());

    }

 

charIConvertible.ToChar(IFormatProvider provider)

    {

returnConvert.ToChar(GetDoubleValue());

    }

 

DateTimeIConvertible.ToDateTime(IFormatProvider provider)

    {

returnConvert.ToDateTime(GetDoubleValue());

    }

 

decimalIConvertible.ToDecimal(IFormatProvider provider)

    {

returnConvert.ToDecimal(GetDoubleValue());

    }

 

doubleIConvertible.ToDouble(IFormatProvider provider)

    {

returnGetDoubleValue();

    }

 

shortIConvertible.ToInt16(IFormatProvider provider)

    {

returnConvert.ToInt16(GetDoubleValue());

    }

 

intIConvertible.ToInt32(IFormatProvider provider)

    {

returnConvert.ToInt32(GetDoubleValue());

    }

 

longIConvertible.ToInt64(IFormatProvider provider)

    {

returnConvert.ToInt64(GetDoubleValue());

    }

 

sbyteIConvertible.ToSByte(IFormatProvider provider)

    {

returnConvert.ToSByte(GetDoubleValue());

    }

 

floatIConvertible.ToSingle(IFormatProvider provider)

    {

returnConvert.ToSingle(GetDoubleValue());

    }

 

stringIConvertible.ToString(IFormatProvider provider)

    {

returnString.Format("({0}, {1})", number1, number2);

    }

 

objectIConvertible.ToType(TypeconversionType, IFormatProvider provider)

    {

returnConvert.ChangeType(GetDoubleValue(),conversionType);

    }

 

ushortIConvertible.ToUInt16(IFormatProvider provider)

    {

returnConvert.ToUInt16(GetDoubleValue());

    }

 

uintIConvertible.ToUInt32(IFormatProvider provider)

    {

returnConvert.ToUInt32(GetDoubleValue());

    }

 

ulongIConvertible.ToUInt64(IFormatProvider provider)

    {

returnConvert.ToUInt64(GetDoubleValue());

    }

 

 

staticvoid Main(string[] args)

        {

ComplextestComplex = newComplex(4, 7);

 

Console.WriteLine(Convert.ToBoolean(testComplex));

Console.WriteLine(Convert.ToDouble(testComplex));

Console.ReadLine();

        }

    }

Add A Comment: