Method Overloading in C# (with Examples)

Learn via video courses
Topics Covered

Overview

Method overloading is a concept in object-oriented programming where multiple methods with the same name but different parameters are defined within a class. Method overloading provides various ways to invoke the same method based on the type or number of arguments passed.

The compiler determines which method to execute based on the arguments provided during the method call. By overloading methods, you can create more flexible and reusable code that can handle various input scenarios without needing to create separate method names for each variation.

Why Do We Need It?

Method overloading allows us to define multiple methods with the same name but different parameters. It enhances code readability by providing a clear and concise representation of similar operations. It offers flexibility and convenience, as developers can use a single method name to handle various input types. Method overloading promotes code reuse by eliminating the need for multiple method names with similar functionality.

Variations of Method Overloading

Method overloading can be done in the following ways:-

  • By changing the number of parameters in methods.
  • By changing the data types of the parameters of methods.
  • By changing the Order of the parameters of methods.

Method Overloading by Changing the Number of Parameters

Method overloading allows one to have more than one method in a class with the same name by changing the number of parameters. In the below class Scaler, we have three methods, i.e., AddMethod(), with the same name because the number of parameters in the method is different.

Output

In the above example, we have overloaded the AddMethod() method:

  • The first AddMethod() method takes two integer parameters and calculates their sum.
  • The second AddMethod() method takes three integer parameters and calculates their sum.
  • The third AddMethod() method takes four integer parameters and calculates their sum.

The corresponding method is called based on the number of parameters given during the method call.

  • obj.AddMethod(5, 6) - calls the method with two parameters, and we will get the output sum : 11.
  • obj.AddMethod(5, 6, 7)- calls the method with three parameters, and we will get the output sum : 18.
  • obj.AddMethod(5, 6, 7, 8) - calls the method with four parameters, and we will get the output sum : 26.

Method Overloading by Changing the Data Types of the Parameters

Method overloading allows one to have more than one method in a class with the same name by changing the data types of parameters. In the below class Scaler, we have three methods, i.e., AddMethod(), with the same name because the data types of parameters in the method are different.

Output

In the above program, we have overloaded the AddMethod() method with different types of parameters.

  • The first AddMethod() method takes three integer parameters and calculates their sum.
  • The second AddMethod() method takes three double parameters and calculates their sum.
  • The third AddMethod() method takes two float parameters and calculates their sum.

The corresponding method is called based on the type of arguments passed during the method call.

  • obj.AddMethod(5, 6, 7) - calls method with int type parameter, and we will get output integer : 18.
  • obj.AddMethod(5.0, 6.0, 7.0) - calls method with double type parameter, and we will get output double : 18.
  • obj.AddMethod(12.4f, 21.3f) - calls method with double type parameter, and we will get output float : 33.7.

Method Overloading by Changing the Order of Parameters

Method overloading allows one to have more than one method in a class with the same name by changing the order of the parameters. In the below class Scaler, we have two methods, i.e., Student(), with the same name because the order of the parameters in the method is different.

Output

In the above program, we have overloaded the Student() method with different orders of parameters.

  • The first Student() method takes string as first parameter and int as second parameter.
  • The second Student() method takes int as first parameter and string as second parameter.

The corresponding method is called based on the order of the arguments passed during the method call.

  • obj.Student("Aditya", 2) - calls the method with string and int parameters, respectively, and we will get the output My name is Aditya and Roll no is 2.
  • obj.Student(1, "Abhinav") - calls method with int and string parameters respectively, and we will get the output My name is Abhinav and Roll no is 1.

What if Method Signature is Same and Return Type is Different?

When the method signature is the same but the return type is different, it is not considered a valid method overloading scenario. The return type alone does not differentiate between methods, so the compiler will throw an error due to the ambiguity in identifying the correct method to call.

Example

Output

In the above program, we have overloaded the AddMethod() method with the same method name but a different return type. One AddMethod takes two integers as input and returns their sum as an integer, while the other AddMethod takes two integers but returns their sum as a double.

The output is an error message stating that the type 'Scaler' already defines a member called 'AddMethod' with the same parameter types. This occurs because C# does not allow overloading methods based solely on their return type, and both AddMethod functions have the same parameter types (int and int).

FAQs

Q: What is method overloading in C#?
A: Method overloading in C# enables a class to contain multiple methods sharing the same name while differing in their parameters. These methods can perform similar operations but with different inputs.

Q: How is method overloading resolved in C#?
A: Method overloading in C# is resolved at compile time. The compiler determines which overloaded method to call based on the number, types, and order of the arguments passed to the method. It selects the most appropriate method by matching the argument types with the available overloaded method signatures. If an exact match is found, that method is called. If an exact match is not found, the compiler tries to perform implicit conversions to find a compatible match.

Q: Is method overloading an example of compile-time polymorphism or run-time polymorphism?
A: Method overloading in C# is an example of compile-time polymorphism, also known as static polymorphism. The selection of the appropriate overloaded method is determined at compile time-based on the number, types, and order of the arguments passed to the method.

Conclusion

  • By providing multiple methods with the same name but different parameter lists, it becomes easier to understand and maintain code.
  • Developers can choose the appropriate method based on the type and number of arguments passed, allowing for greater flexibility in function usage.
  • Method overloading enables the reuse of method names, promoting code reusability and reducing duplication of code. Overall, method overloading enhances the efficiency and organization of C# programs.