Method Overloading. Method Overloading (Polymorphism) in C#
Tags: Learning, Csharp, Programming, Function, Tutorial

Method Overloading (Polymorphism) in C#

In this blog, I am going to explain what is Method Overloading, also known as Polymorphism.

Let’s say we want two different methods with the same name. If we define methods like this, it would give us compiler error:

CS0111: Type “Class_Name” already defines a member called “MyMethod” with the same parameter types

But how can we handle this error ?

So, this is where Method Overloading would be useful. Basically, Method Overloading means “defining method with the same name in different forms.” They can be overloaded in 3 different ways:

Having a different number of parameters

Having different types of parameters

Having a different order of parameters

These may confuse you, but you do not need to worry about it. You will understand them as you progress.

Thus, ow that we have some basic information, we can get started to understand them.

Having different numbers of parameters

Let’s suppose we have methods called MyMethod with two different variants. The only difference is that their number of parameters is different. Let’s see the example:

public int MyMethod(int number)

{

return number;

}

public int MyMethod(int firstNumber, int secondNumber)

{

return firstNumber + secondNumber;

}

As you see, both of the methods have the same name, but their parameters are different. The first one takes a parameter with the type of integer called number . The second one takes two parameters with the type of integer called firstNumber and secondNumber As a result, there is no compiler error and they are working all good.

So, we understood the first part of the Polymorphism. If you didn’t have any problem with this part, the other forms of Method Overloading would be simpler.

Now, let’s look at the second way of Method Overloading

Having different types of parameters

In this way of Polymorphism, each method will take one parameter, but their data types will be different.

Note: I am using int and string data types, but do not forget you can use any two different types

In the example above, as always, we have the same method name. The first method takes an integer value called number , but the other one takes a string value called name.

You can see how those two ways of Polymorphism look like each other. Similarly, the third way will be as simple as the first two ones.

To see how much they are similar, continue to read about the third variant of Method Overloading.

Having different order of parameters

In the first variant, we had a different number of parameters. In the second variant, we had different types of parameters. Now, we will have the same number of parameters, but they will be in a different order.

To understand, see the following:

int myNumber;public void MyMethod(int number, string name)

string myName;

{

myNumber = number;

myName = name;

}

public void MyMethod(string name, int number)

{

myName = name;

myNumber = number;

}

As you see, they take the same parameters in a different order. First of them takes number then takes name . The Second one is doing this process in reverse order. That is, firstly, it takes the name , secondly, it takes the number.

Conclusion

To sum up, Method Overloading is one of the most important topics in programming. Imagine if you do not use this, you would end up thinking of different names for the methods which are doing similar tasks. As a result, we understood what is the Method Overloading and how is it used. I hope this helped you and you liked it.