Monday, April 6, 2015

Different Types of Type Casting/Type Conversions in C#

  1. Implicit conversion

  2. Explicit conversion

  3. User-defined conversion 

Implicit conversion

Implicit conversion is being done automatically by the compiler and no data will be lost. It includes conversion of a smaller data type to a larger data types and conversion of derived classes to base class. This is a safe type conversion.

Example: Converting an int to a float will not loose any data and no exception will be thrown, hence an implicit conversion can be done. 
Where as when converting a float to an int, we loose the fractional part and also a possibility of overflow exception. Hence, in this case an explicit conversion is required. For explicit conversion we can use cast operator or the convert class in c#.

1. Implicit Conversion Exampleusing System;class MyProgram{
    public static void Main()
    {
        int num = 10;

        // float is always bigger data type than int. So, there will be no loss of
        // data and exceptions. Hence implicit conversion take place here
        float f = num;

        Console.WriteLine(f);
    }
}

 

2. Explicit conversion

Explicit conversion is being done by using a cast operator. It includes conversion of larger data type to smaller data type and conversion of base class to derived classes. In this conversion information might be lost or conversion might not be succeed for some reasons. This is an un-safe type conversion.

Exampleusing System;class MyProgram{
    public static void Main()
    {
        float myFloat = 125.32F;

        // Cannot implicitly convert float to int.
        // Fractional part will be lost. Again Float is a
        // highter datatype than int, so there is
        // also a possibility of overflow exception
        // int num = myFloat ;

        // Use explicit conversion using cast () operator
        int num = (int)myFloat ;

        // OR use Convert class
        // int num = Convert.ToInt32(myFloat );

        Console.WriteLine(num);
    }
}

 

3. User-defined conversion

User-defined conversion is performed by using special methods that you can define to enable explicit and implicit conversions. It includes conversion of class to struct or basic data type and struct to class or basic data type. Also, all conversions methods must be declared as static.

    class RationalNumber
    {
    int numerator;
    int denominator;
    
    public RationalNumber(int num, int den)
    {
    numerator = num;
    denominator = den;
    }
    
    public static implicit operator RationalNumber(int i)
    {
    // Rational Number equivalant of an int type has 1 as denominator
    RationalNumber rationalnum = new RationalNumber(i, 1);
    return rationalnum;
    }
    
    public static explicit operator float(RationalNumber r)
    {
    float result = ((float)r.numerator) / r.denominator;
    return result;
    }
    
    }
    class Program
    {
    static void Main(string[] args)
    {
    // Implicit Conversion from int to rational number
    RationalNumber rational1 = 23;
    
    //Explicit Conversion from rational number to float
    RationalNumber rational2 = new RationalNumber(3, 2);
    float d = (float)rational2;
    }
    }

 

 

No comments:

Post a Comment