Author avatar

Gaurav Singhal

Overload Methods and Invoking Overloaded Methods in C#

Gaurav Singhal

  • Jul 26, 2019
  • 4 Min read
  • 622 Views
  • Jul 26, 2019
  • 4 Min read
  • 622 Views
Languages Frameworks and Tools
C#

Introduction

Method overloading is a salient feature in Object-Oriented Programming (OOP). It lets you declare the same method multiple times with different argument lists. In this guide, we are going to discuss how we can achieve method overloading in C#.

Overloading

Method overloading is a form of polymorphism in OOP. Polymorphism allows objects or methods to act in different ways, according to the means in which they are used. One such manner in which the methods behave according to their argument types and number of arguments is method overloading.

Overloading happens when you have two methods with the same name but different signatures (or arguments). In a class we can implement two or more methods with the same name. Overloaded methods are differentiated based on the number and type of parameter passed as arguments to the methods. If we try to define more than one method with the same name and the same number of arguments then the compiler will throw an error.

The advantage of method overloading is that it increases code readability and maintainability. Although it is possible to have methods with the same name that perform a totally different function, it is advised that overloaded methods must have similarities in the way they perform.

Overloading Methods

It’s very easy to create a class with overloaded methods, just define methods with the same name but with different argument lists.

Method overloading can be achieved by the following:

  • By changing the number of parameters in a method

  • By changing the order of parameters in a method

  • By using different data types for parameters

Let’s look at a very common example, to find the area of any polygon.

1
2
3
4
5
6
7
8
9
10
11
public class Area {
    public double area(double s) {
        double area = s * s;
        return area;
    }

    public double area(double l, double b) {
        double area = l * b;
        return area;
    }
}
csharp

In the above code, the method area() is defined twice. First, it's defined with one argument to find the area of the square; and second, it's defined with two arguments length and breadth, to find the area of the rectangle.,

Invoking Overloaded Methods

To invoke the overloaded methods, call the method with the exact arguments. For example, if we want to invoke the area() method to find the area of a square, we will pass only one argument.

1
2
3
4
Area a = new Area();
double side = 3.3;
double square = a.area(side);
Console.WriteLine(square);
csharp

Output:

1
10.89
console

Similarly to find the area of the rectangle we would want to write the following,

1
2
3
4
5
Area a = new Area();
double length = 3.3;
double breadth = 4.9;
double rect = a.area(length, breadth);
Console.WriteLine(rect);
csharp

Output:

1
16.17
console

Complete Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using System;

namespace Overload_methods_and_invoke {
    public class Area {
        public double area(double s) {
            double area = s * s;
            return area;
        }

        public double area(double l, double b) {
            double area = l * b;
            return area;
        }
    }

    class Program {
        public static void Main(string[] args) {
            Area a = new Area();
            double length = 3.3;
            double breadth = 4.9;
            double rect = a.area(length, breadth);
            Console.WriteLine("Area of rectangle " + rect);

            double side = 3.3;
            double square = a.area(side);
            Console.WriteLine(square);
        }
    }
}
csharp

Output:

1
2
Area of rectangle 16.17
Are of square 10.89
console

Conclusion

Method overloading is a great feature for building applications that are easy to maintain. Having said that - with great power comes great responsibility - right? Too much method overloading will eventually make the code difficult to maintain for other developers. I hope you got a fair idea about method overloading. Happy coding!

1