Language: EN

csharp-sobrecarga-funciones

What is function overloading in C#

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 to invoke based on the arguments provided during the call.

This provides flexibility and improves code readability by allowing the same method to adapt to different scenarios.

Syntax of overloading

To use function overloading, the different versions of the function must have different arguments. That is, they must receive 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 to sum two integers
  • Another to sum three integers
  • Another to sum 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 a single 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 integers
public static int Sumar(int a, int b)
{
    return a + b; // Return the sum of two integers
}

// 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 integers
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}");

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("Juan", 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 problems they address. Typically, there are better alternatives.