Method Overloading in C#
Jul 20, 2015 02:00 0 Comments C# (C-Sharp) Pradip

Method Overloading in C#

 

Method Overloading is the ability to redefine a function in more than one form. A user can implement function overloading by defining two or more functions in a class sharing the same name.

 

C# can distinguish the methods with different method signatures. i.e. the methods can have the same name but with different parameters list (i.e. the number of the parameters, order of the parameters, and data types of the parameters) within the same class.

 

·         Overloaded methods are differentiated based on the number and type of the parameters passed as arguments to the methods.

·         You cannot define more than one method with the same name, Order and the type of the arguments. It would be compiler error.

·         The compiler does not consider the return type while differentiating the overloaded method. But you cannot declare two methods with the same signature and different return type. It will throw a compile-time error. If both methods have the same parameter types, but different return type, then it is not possible.

 

Why do we need Method Overloading ?

 

If we need to do the same kind of the operation in different ways i.e. for different inputs. In the example described below, we are doing the addition operation for different inputs. It is hard to find many different meaningful names for single action.

 

Different ways of doing method overloading –

 

1.       The number of parameters in two methods.

2.       The data types of the parameters of methods.

3.       The order of the parameters of methods.

 

1.      By changing the number of parameters

 

using System;

class Program {

 

    // adding two integer values.

    public int Add(int a, int b)

    {

        int sum = a + b;

        return sum;

    }

 

    // adding three integer values.

    public int Add(int a, int b, int c)

    {

        int sum = a + b + c;

        return sum;

    }

  

    // Main Method

    public static void Main(String[] args)

    {

   // Creating Object

        Program obj = new Program();

      int sum1 = obj.Add(1, 2);

        Console.WriteLine("sum of the two "

                          + "integer values : " + sum1);

  

        int sum2 = obj.Add(1, 2, 3);

        Console.WriteLine("sum of the three "

                          + "integer values : " + sum2);

    }

}

 Output:

Sum of the two integer values: 3

Sum of the three integer values: 6

 2.      By changing the data types of the parameters

 using System;

class Program {

  // adding three integer values.

    public int Add(int a, int b, int c)

    {

        int sum = a + b + c;

        return sum;

    }

  // adding three double values.

    public double Add(double a,

                      double b, double c)

    {

       double sum = a + b + c;

        return sum;

    }

   // Main Method

    public static void Main(String[] args)

    {

   // Creating Object

        Program obj = new Program();

 int sum2 = obj.Add(1, 2, 3);

        Console.WriteLine("sum of the three "

                          + "integer values : " + sum2);

    double sum3 = obj.Add(1.0, 2.0, 3.0);

        Console.WriteLine("sum of the three "

                          + "double values : " + sum3);

 

Prev Next
About the Author
Topic Replies (0)
Leave a Reply
Guest User

You might also like

Not sure what course is right for you?

Choose the right course for you.
Get the help of our experts and find a course that best suits your needs.


Let`s Connect