Method Overloading in c#

Method overloading means method with same name and different signature. We can implement Method overloading same method name with different type of parameters or different set of parameters.

Let’s take an example of Addition in C#. Please see below code snippet for more understanding. If we don’t have idea about how many parameters are passed to the method 2 or 3 and don’t know the type of data passed to the method. So we define multiple methods where for every requirement have their own implementation like below:

using System;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 2,
                b = 3,
                c = 4;

            float p = 3.4f,
                q = 7.8f,
                r = 9.10f;

            MethodOverloading Emp = new MethodOverloading();

            Emp.Add(a, b);
            Emp.Add(a, b, c);
            Emp.Add(p, q);
            Emp.Add(p, q, r);


            Console.ReadLine();
        }
    }

    public class MethodOverloading
    {
        public void Add(int a, int b)
        {
            Console.Write("Addition of Two Integer Parameters={0}\n", a + b);
        }

        public void Add(int a, int b, int c)
        {
            Console.Write("Addition of Three Integer Parameters={0}\n", a + b + c);
        }

        public void Add(float a, float b)
        {
            Console.Write("Addition of Two float Parameters={0}\n", a + b);
        }

        public void Add(float a, float b, float c)
        {
            Console.Write("Addition of Three float Parameters={0}\n", a + b + c);
        }
    }
}


Output:
Addition of Two Integer Parameters=5
Addition of Three Integer Parameters=9
Addition of Two float Parameters=11.2
Addition of Three float Parameters=20.3

We can override out and ref parameters also. Many interviewers asked about this, can we implement method overloading for out and ref parameter? Yes. Like below code. Reference parameter is also like out parameter so we can implement method overloading for ref parameters also.

using System;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 2,
                b = 3,
                c = 4;

            float p = 3.4f,
                q = 7.8f,
                r = 9.10f;

            string outparameter = null;

            MethodOverloading Emp = new MethodOverloading();

            Emp.Add(a, b);

            Emp.Add(a, b,out outparameter);

            Console.Write(outparameter);

            Console.ReadLine();
        }
    }

    public class MethodOverloading
    {
        public void Add(int a, int b)
        {
            Console.Write("Addition of Two Integer Parameters={0}\n", a + b);
        }

        public void Add(int a, int b, out string output)
        {
            output = string.Format("Addition of Two Integer Parameters ----- output Parameter ={0}\n", a + b);
        }
    }
}

Output:
Addition of Two Integer Parameters=5
Addition of Two Integer Parameters ----- output Parameter =5





Post a Comment

0 Comments