Thursday, May 1, 2014

Function Overloading

class FutionOverLoading
    {
        static int t;
        int[] numbers = { 1, 2, 3, 4, 5, 6 };
        int[] arr;
        public static void Main() {
            FutionOverLoading f = new FutionOverLoading();
            f.ADD(1, 3);//public void ADD(int i, int b)
            f.ADD(false, 5);// public void ADD(int i, bool b)
            f.ADD(3, true);// public void ADD(bool i, int b)
            f.ADD(1,2,3);//public void ADD(int i, int b, int c)
            f.ADD(2, 2,out  t);// public int ADD(int i, int b, out int sum)
            f.sum(out t, 1);//sum(out int num1, int num2)
            int y = 5;
            f.sum(ref y, "amit");//public void sum(ref int num1, string s)

            f.arr = new int[4];
            for (int i = 0; i < f.arr.Length; i++)
            {
                f.arr[i] = i+1;
            }
            f.ADD(2, f.arr);//  public void ADD(int i, int[] t)
            f.ADD(1,2,3,4,5);//public void ADD(params int[] t)
            f.ADD(f.numbers);//public void ADD(params int[] t)
            f.ADD(true ,4,f.arr);// public void ADD(bool h,int f,params int[] t)
            f.ADD(true, 4, 1,2,3,4);//that is why params shud be at last  // public void ADD(bool h,int f,params int[] t)
            f.sum(4, 6, 6, 7, 8, 8);//   public void sum(int  f,params int[] t)
            Foo(5);// Foo(int x)
            Foo(5,6);//  static void Foo(int x, int y = 5) optional parameret
            double tee = 55;
            Foo(5, tee);//    static void Foo(int x, double y)
            Foo(tee, 4);//Foo(double x, int y)
            FutionOverLoading s = new FutionOverLoading();
            s.print();
            Console.ReadLine();


        }

        int y;
        public void sum(out int num1, int num2)
        {
            Console.WriteLine("*****Inside OUT*******");
            num1 = 9;      //The out parameter 'num1' must be assigned to before control leaves the current method      
            Console.WriteLine(num1);//9
            Console.WriteLine(num2);//10
        }
        public void sum(ref int num1, string s)
        {
            Console.WriteLine("*****Inside Ref*******");
            Console.WriteLine(num1); //0
            Console.WriteLine(s);//ammit
        }

        public void F(int m)
        {

        }
        public void print()
        {
            int i;
            sum(out i, 10);
            Console.WriteLine("Value of out");
            Console.WriteLine(i);//9
            F(y);
            sum(ref  y, "ammit");
            Console.WriteLine("Value of ref y is" + y);//0
            int x = 7; //For Ref it is manadatory to initilize before passing.
            sum(ref  x, "ammit");
            Console.WriteLine("Value of ref x is" + x);//7

            Console.WriteLine(y);//0
        }
        public void ADD(int i, int b) { Console.WriteLine("ADD(int i, int b)"); }
        public void ADD(int i, bool b) { Console.WriteLine("ADD(int i, bool b)"); }
        public void ADD(bool i, int b) { Console.WriteLine("ADD(bool i, int b)"); }
        public void ADD(int i, int b, int c) { Console.WriteLine("ADD(int i, int b, int c)"); }
        public int ADD(int i, int b, out int sum) { Console.WriteLine("ADD(int i, int b, out int sum) "); return sum = i + b; }//before getting out from method block out shud be initilaized.
       // public float ADD(int i, int b, ref int sum) { return sum; }//in ref befor going in to//Error 1       Cannot define overloaded method 'ADD' because it differs from another method only on ref and out      
        public void ADD(int i, int[] t) { Console.WriteLine("ADD(int i, int[] t)"); } // before calling you know the size of the size of the int array, but with params It is used when we don't know the number of parameters will be passed to the called method.
       // public void ADD(int i, params int[] t) { }//Error   1      Type 'KudVenkatCsharp.FutionOverLoading' already defines a member called 'ADD' with the same parameter types      
        //above is not possible becoz only diff is of params
        public void ADD(params int[] t)
        {
            foreach (int x in t)
            {
                Console.WriteLine(" " + x);
            }
            Console.WriteLine("ADD(params int[] t)");
        }
        public void ADD(bool h,int f,params int[] t)
        {
            foreach (int x in t)
            {
                Console.WriteLine(" " + x);
            }
            Console.WriteLine("ADD(bool h,int f,params int[] t)");
        }
        public void sum(int  f,params int[] t)
        {
            Console.WriteLine(f);
            foreach (int x in t)
            {
                Console.WriteLine(" " + x);
            }
            Console.WriteLine(" sum(int  f,params int[] t)");
        }
        static void Foo(int x, int y = 5)
        {
            Console.WriteLine("Foo(int x, int y = 5)");

        }

        static void Foo(int x)
        {
            Console.WriteLine("Foo(int x)");
        }
        static void Foo(int x, double y)
        {
            Console.WriteLine("Foo(int x, double y)");
        }

        static void Foo(double x, int y)
        {
            Console.WriteLine("Foo(double x, int y)");
        }



    }







No comments:

Post a Comment