In this article, we are going to learn about method overloading in C#.
In objected-oriented programming, method overloading is one of the important concepts to understand. It is a type of polymorphism that allows us to define methods that exist in different forms. Method overloading is simple to implement and has a lot of benefits for our code. So, we will discuss what it means, how to implement it, and what we stand to gain from it.
Let’s dive in.
What Is Method Overloading?
Method overloading is the technique that allows the creation of different methods in a class with the same name. Overloading exists between methods when they have the same name but differ in the number of parameters, type of parameters, or order of the parameters. When calling an overloaded method, C# chooses the correct method by comparing the list of arguments entered to match the appropriate implementation.
How to Perform Method Overloading in C#?
We achieve method overloading in three ways:
- Different number of parameters
- Different types of parameters
- Different order of parameters
Let’s discuss them.
Methods With a Difference in the Number of Parameters
We can have multiple methods with the same name but a different number of parameters. To show that, let’s declare two methods that have the same name with the first method having two parameters and the second having more than two parameters:
public class Overload { public int AddNumbers(int num1, int num2) { return num1 + num2; } public double AddNumbers(int num1, int num2, int num3) { return num1 + num2 + num3; } }
We have two methods both named AddNumbers
. While the first method has two parameters, the second one has three. C# decides what method to call based on the parameters we pass when calling the methods:
public class Program { public static void Main(string[] args) { var overload = new Overload(); var sum1 = overload.AddNumbers(1, 2); //sum1 is 3 var sum2 = overload.AddNumbers(1, 2, 3); //sum2 is 6 } }
Methods With a Difference in the Type of Parameters
We can declare multiple methods with the same name but with a difference in the type of parameters they accept. Let’s declare two methods that have the same name with the first method taking an int
parameter, and the second taking a string
parameter:
public class Overload { public string Append(int numb) { return $"Value is: {numb}"; } public string Append(string numb) { return $"Value is: {numb}"; } }
When we call Append
from the Main
method and pass an integer
, C# knows the right method to call:
public class Program { public static void Main(string[] args) { var overload = new Overload(); var obj1 = overload.Append(1); var obj2 = overload.Append("one"); } }
Methods With a Difference in the Order of Parameters
We can declare two or more methods with the same name but with a difference in the order of the parameters they accept:
public class Overload { public string Order(int numb, string item) { return item + numb; } public string Order(string item, int numb) { return item + numb; } }
Here we define two methods that have the same name. Our first method takes an int
and string
parameter in that order and the second overloaded method takes a string
and int
thereby changing the order compared to the first.
Based on what we pass when calling the Order
method, C# knows the right method to implement:
public class Program { public static void Main(string[] args) { var overload = new Overload(); var val1 = overload.Order(1, "item"); var val2 = overload.Order("item", 2); } }
It is worth noting that the return type of overloaded methods doesn’t have to be the same. C# allows overloaded methods to have different return types. For example, our second Order method could return any other type instead of a string, and everything will work fine. However, we can not have methods with the same parameter(s) but different return types. The C# compiler will indicate an error.Â
Inheritance Based Overloading
When we have a class that derives from a base class – inheritance between classes, we can define an overloaded method of the base class in the derived class.
Let’s add the base class first:
public class Base { public int AddNumbers(int num1, int num2) { return num1 + num2; } }
And then, the derived one:
public class Derived : Base { public int AddNumbers(int num1, int num2, int num3) { return num1 + num2 + num3; } }
Here the base class has a method named AddNumbers
that takes two parameters. The derived class has a method with the same name but with an additional parameter. When we call the methods from the main function and pass in the values, C# will execute the correct implementation:
public class Program { public static void Main(string[] args) { var derived = new Derived(); var sum3 = derived.AddNumbers(1, 2); //sum3 is 3 var sum4 = derived.AddNumbers(1, 2, 3); //sum3 is 6 } }
Benefits Of Method Overloading in C#
There are several benefits of method overloading:
- It gives us the flexibility to use a different implementation calling the same method name
- When properly implemented, it makes our code maintainable
- It increases the readability of our code
- We can use it on constructors to create new objects given different types of data
Conclusion
In this article, we discussed method overloading, the different ways to implement it, and what are the benefits of implementing it in our code.