Method Overloading in C#

Methods can be overloaded in C# in any of the two ways:

  • By specifying different number of parameters.
  • By specifying different types of parameters.

Overloading on the return type of method is not supported in C#.

While discussing constructors, in Example 2 did you notice that both the constructors had the same name but they both could co-exist? Well, if you notice they both have the same name but one takes in parameters, the other does not. This is how they are differentiated. In Example 2, unknowingly you have implemented method overloading, Constructor Overloading to be precise. Method overloading is done in a similar way.

1.   Method overloading using different number of parameters

Consider Example 5,

Example 5:

Implementing OOP Concepts in C# Example 5

Though in Example 5 all three methods have the same name they are considered to be different as we have overloaded them using different number of parameters.

2.   Method overloading using different types of parameters

The other way to overload methods is by specifying different types of parameters. In this kind of method overloading the number of parameters passed does not matter at all. They can even remain the same.

Consider Example 6,

Example 6:

Implementing OOP Concepts in C# Example 6

Though in Example 6 all methods have the same name and the same number of parameters are passed to them, they are considered to be different just because the parameters passed to them are of different data types. (The first Admission method takes in parameters of type short and string, whereas the second Admission method takes in parameters of type int and string and the third Admission method takes in parameters of type boolean and int.)

Leave a comment