Function overloading is a feature in C# that allows us to define multiple versions of methods with the same name, but different parameters.
The C# compiler determines which version of the method should be invoked based on the arguments provided during the call.
This provides flexibility and improves code readability by allowing a single method to adapt to different scenarios.
If you want to learn more about Function Overloading
check out the Introduction to Programming Course read more
Overloading Syntax
To use function overloading, the different versions of the function must have different arguments. That is, they must accept different types and/or quantities of parameters.
// Sum of two integers
public int Sumar(int a, int b)
{
return a + b;
}
// Sum of three integers
public int Sumar(int a, int b, int c)
{
return a + b + c;
}
// Sum of two floating-point numbers
public double Sumar(double a, double b)
{
return a + b;
}
In this example, the Sumar
method is overloaded three times:
- One for summing two integers
- Another for summing three integers
- Another for summing two floating-point numbers.
It is not possible to use overloading based on the return type of the function. Only the parameters are involved.
For example, the following case, where we have two MiMetodo
with the same number of parameters but different return
, is not valid.
public int MiMetodo(int a, int b)
{
}
// this cannot be done
public double MiMetodo(int a, int b)
{
}
Practical Examples
Calculate the area of a rectangle
In this example, it shows how to overload a function to calculate the area of a rectangle using different sets of parameters.
// Function to calculate the area of a rectangle using length and width
public static double CalcularArea(double longitud, double ancho)
{
return longitud * ancho; // Return the area of the rectangle
}
// Function to calculate the area of a rectangle using one side (square)
public static double CalcularArea(double lado)
{
return lado * lado; // Return the area of the square
}
// Usage
double areaRectangulo = CalcularArea(5.0, 3.0); // Call the function with two parameters
double areaCuadrado = CalcularArea(4.0); // Call the function with one parameter
// Print the results
Console.WriteLine($"Area of the rectangle: {areaRectangulo}");
Console.WriteLine($"Area of the square: {areaCuadrado}");
Overloading to sum numbers
In this example, it shows how to overload a function to sum numbers of different types: integers and decimals (double).
// Function to sum two integer numbers
public static int Sumar(int a, int b)
{
return a + b; // Return the sum of two integer numbers
}
// Function to sum two decimal numbers (double)
public static double Sumar(double a, double b)
{
return a + b; // Return the sum of two decimal numbers
}
// Usage
int sumaEnteros = Sumar(3, 5); // Call the function with two integer numbers
double sumaDecimales = Sumar(3.5, 5.7); // Call the function with two decimal numbers
// Print the results
Console.WriteLine($"Sum of integers: {sumaEnteros}");
Console.WriteLine($"Sum of decimals: {sumaDecimales}");
Print employee information
In this example, it shows how to overload a function to print employee information using different sets of parameters.
// Function to print employee information using name and age
public static void ImprimirEmpleado(string nombre, int edad)
{
Console.WriteLine($"Employee: {nombre}, Age: {edad}"); // Print name and age
}
// Function to print employee information using name, age, and department
public static void ImprimirEmpleado(string nombre, int edad, string departamento)
{
Console.WriteLine($"Employee: {nombre}, Age: {edad}, Department: {departamento}"); // Print name, age, and department
}
// Usage
ImprimirEmpleado("Luis", 30); // Call the function with two parameters
ImprimirEmpleado("María", 25, "Sales"); // Call the function with three parameters
These examples are intended to show how to use overloading. It does not mean that it is the best way to solve the problem they address. Usually, there are better alternatives.