Method Overloading in Programming

This post refers to the previous one we saw about polymorphism since in that post we discussed what is known as static polymorphism.

We can define method overloading as polymorphism at runtime.

 

1 - What is method overloading?

Method overloading means that we have multiple methods within a class that have the same name but different parameters.

These parameters can vary in several ways:

  1. Number of parameters
  2. Type of parameters
  3. Order of parameters

Keep in mind that we cannot define two identical methods, meaning with the same name and the same parameters in the same order, as this would result in a compilation error.

 

 

2 - Method overloading example in C#

The easiest way to see this is with an example.

We have a class that performs a sum to which we pass two values.

public int Suma(int item1, int item2)
{
    return item1 + item2;
}

Now, if we want to send 3 parameters instead of 2, instead of changing the names, which would be bad practice, we create another method with the same name but pass three parameters instead of two.

public int Suma(int item1, int item2)
{
    return item1 + item2;
}

public int Suma(int item1, int item2, int item3)
{
    return item1 + item2 + item3;
}

And as I've mentioned before, we can also have the same number of parameters but with different types. The compiler knows which method to use and doesn't generate any problem for us.

public int Suma(int item1, int item2)
{
    return item1 + item2;
}
public string Suma(string item1, int item2)
{
    return $"{item2} sumado a {item1}";
}

As we can see, when running the code, it works without issues. Also, when we are writing the method we want to use, Visual Studio detects the overload and doesn't show us multiple lines to select the method from. Instead, it shows us only one and indicates that there are two more overloaded methods.

method overloading oop

Once you select the method, you can iterate through the different overloads with the up and down arrow keys.

method overloading oop

And this would be the example of calling both methods, which works without any problem

int resultadoSuma1 = ejemploSobrecarga.Suma(1, 2);
int resultadoSuma2 = ejemploSobrecarga.Suma(1, 2, 3);

That's how we implement method overloading in C#, defining multiple methods with the same name but different parameters according to the requirements we need.

The code we have used in this course is available on github at the following link: github link

This post was translated from Spanish. You can see the original one here.
If there is any problem you can add a comment bellow or contact me in the website's contact form

Uso del bloqueador de anuncios adblock

Hola!

Primero de todo bienvenido a la web de NetMentor donde podrás aprender programación en C# y .NET desde un nivel de principiante hasta más avanzado.


Yo entiendo que utilices un bloqueador de anuncios como AdBlock, Ublock o el propio navegador Brave. Pero te tengo que pedir por favor que desactives el bloqueador para esta web.


Intento personalmente no poner mucha publicidad, la justa para pagar el servidor y por supuesto que no sea intrusiva; Si pese a ello piensas que es intrusiva siempre me puedes escribir por privado o por Twitter a @NetMentorTW.


Si ya lo has desactivado, por favor recarga la página.


Un saludo y muchas gracias por tu colaboración

© copyright 2025 NetMentor | Todos los derechos reservados | RSS Feed

Buy me a coffee Invitame a un café