Tuesday, 4 August 2015

Examples of Implicit and Explicit Casting of datatypes in C#


Case 1 : 
Suppose you have a class, class having some properties/data members.
When you instantiate this class, all the data members of this class will be initialized with it's default values or by null.

  • If data member of class is value type, it will be initialize by its default value like int variable initialize with zero, float with 0.0, bool with false etc.
  • If data member of class is reference type, it will be null like string variable, array, object of other class will also be null.
Please refer following example :

 public class Class1
    {
        public int var_int;
        public string var_string;
        public float var_float;
        public bool var_bool;
        public string[] var_str_arr;
        public int[] var_int_arr;
        public Class2 var_class2;
    }
    public class Class2
    {
        public int var_int;
        public string var_string;
        public float var_float;
    }

  class Program
    {
        static void Main(string[] args)
        {            
            Class1 objclass1 = new Class1();   

            Console.WriteLine(objclass1.var_int);        // Output will be 0              
            Console.WriteLine(objclass1.var_float);      // Output will be 0.0
            Console.WriteLine(objclass1.var_bool); // Output will be false
           Console.WriteLine(objclass1.var_string);    // var_string will be null
            Console.WriteLine(objclass1.var_str_arr); // var_str_arr will be null
            Console.WriteLine(objclass1.var_int_arr); // var_int_arr will be null
            Console.WriteLine(objclass1.var_class2); // var_class2 will be null
            
            Console.ReadLine();
        }
    }
   

Case 2 : Casting of variables

class Program
    {
        static void Main(string[] args)
        {
            int i;
            float flt;
            double dbl;
            decimal deci;

            i = 10;
            flt = 10.10f;
            dbl = 20.20;
            deci = 12.456m;

            Console.WriteLine("...........Convert to int...........");
            //i = flt;                              //Error : can not implicitly convert type float to int
            i = (int)flt;                          //Explicit casting
            Console.WriteLine(i);
            //i = dbl;                            //Error : can not implicitly convert type double to int
            i = (int)dbl;                         //Explicit casting
            Console.WriteLine(i);
            //i = deci;                           //Error : can not implicitly convert type decimal to int
            i = (int)deci;                        //Explicit casting
            Console.WriteLine(i);

            i = 10;
            flt = 10.10f;
            dbl = 20.20;
            deci = 12.456m;

            Console.WriteLine("...........Convert to float...........");
            flt = i;
            Console.WriteLine(flt);
            //flt = dbl;                        //Error : can not implicitly convert type double to float
            flt = (float)dbl;
            Console.WriteLine(flt);
            //flt = deci;                      //Error : can not implicitly convert type decimal to float
            flt = (float)deci;
            Console.WriteLine(flt);

            i = 10;
            flt = 10.10f;
            dbl = 20.20;
            deci = 12.456m;

            Console.WriteLine("...........Convert to double...........");
            dbl = i;
            Console.WriteLine(dbl);
            dbl = flt;                     
            Console.WriteLine(dbl);            
            //dbl = deci;                       //Error : can not implicitly convert type decimal to double
            dbl = (double)deci;
            Console.WriteLine(dbl);

            i = 10;
            flt = 10.10f;
            dbl = 20.20;
            deci = 12.456m;

            Console.WriteLine("...........Convert to decimal...........");
            deci = i;
            Console.WriteLine(deci);
            //deci = flt;                                 //Error : can not implicitly convert type float to decimal
            deci = (decimal)flt;
            Console.WriteLine(deci);
            //deci = dbl;                             //Error : can not implicitly convert type double to decimal
            deci = (decimal)dbl;
            Console.WriteLine(deci);
                                  
            Console.ReadLine();

        }

Output : 

Case 3 :  Calling of function by changing it's argument type also depends on implicit conversion of data types.


class Program
    {
        static void Main(string[] args)
        {
            int i;
            float flt;
            double dbl;
            decimal deci;

            i = 10;
            flt = 10.10f;
            dbl = 20.20;
            deci = 12.456m;

            //******Passing expected argument******
            CheckIntFloatDecimalDouble(i, flt, deci, dbl);          // Work Fine

            //******Changing first(integer) argument******
            //CheckIntFloatDecimalDouble(flt, flt, deci, dbl);      // Error : has some invalid argument

            //******Changing second(float) argument******
            //CheckIntFloatDecimalDouble(i, dbl, deci, dbl);        // Error : has some invalid argument
            CheckIntFloatDecimalDouble(i, i, deci, dbl);            // Work Fine

            //******Changing third(decimal) argument******
            //CheckIntFloatDecimalDouble(i, flt, flt, dbl);         // Error : has some invalid argument
            //CheckIntFloatDecimalDouble(i, flt, dbl, dbl);         // Error : has some invalid argument

            //******Changing fourth(double) argument******
            CheckIntFloatDecimalDouble(i, flt, deci, flt);          // Work Fine
            //CheckIntFloatDecimalDouble(i, flt, deci, deci);       // Error : has some invalid argument
        
            Console.ReadLine();
        }
        public static void CheckIntFloatDecimalDouble(int int_var, float float_var, decimal deci_var, double dbl_var)
        {
            Console.WriteLine(int_var);
            Console.WriteLine(float_var);
            Console.WriteLine(deci_var);
            Console.WriteLine(dbl_var);
        }
    }


No comments:

Post a Comment