Implicit vs Explicit Casting,type conversion in C#

Implicit type conversion


In Implicit type conversion one datatype is automatically converted to other datatype by compliler.





    using System;

    class ConversionTest
{
    public static void Main()
    {
        byte mybyte = 1;
        int myintA = 1234;
        int myintB = mybyte; // this will Implicit cast from byte to int datatypes.
        double DoubleD = myintA; // this is Implicit cast from int  to double datatype.
        Console.WriteLine("{0}", myintB);
        Console.WriteLine("{0}", DoubleD);
    }
}

Explicit type conversion

In explicit type conversion the type conversion  is explicitly defined within a program it is not done by compiler Implicitly.


    double A = 101.2;
    double B = 305.3;
    double C = 306.4;
    int result = (int)A + (int)B + (int)C; //result == 712

Tags: , , ,

Join Us!