Language: EN

csharp-sobrecarga-funciones

What is function overloading in C#

Function overloading is an essential feature in C# that allows defining 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 the same method to adapt to different scenarios.

Basic Example

For this, the functions must have a different “signature”. That is, they must receive different types and/or quantity of parameters.

// Sum of two integers
public int Sum(int a, int b)
{
	return a + b;
}

// Sum of three integers
public int Sum(int a, int b, int c)
{
	return a + b + c;
}

// Sum of two floating point numbers
public double Sum(double a, double b)
{
	return a + b;

In this example, the Sum method is overloaded three times

  • One to add two integers
  • Another to add three integers
  • Another to add two floating point numbers.

It is not possible to use overloading using the return type of the function. Only the parameters are involved.

For example, the following case, where we have two MyMethod with the same number of parameters, but different return, is not valid.

public int MyMethod(int a, int b)
{
}

// this cannot be done
public double MyMethod(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 parameter sets.

// Function to calculate the area of a rectangle using length and width
public static double CalculateArea(double length, double width)
{
    return length * width; // Return the area of the rectangle
}

// Function to calculate the area of a rectangle using a single side (square)
public static double CalculateArea(double side)
{
    return side * side; // Return the area of the square
}

// Use
double rectangleArea = CalculateArea(5.0, 3.0); // Call the function with two parameters
double squareArea = CalculateArea(4.0); // Call the function with one parameter

// Print the results
Console.WriteLine($"Rectangle area: {rectangleArea}");
Console.WriteLine($"Square area: {squareArea}");

Overload to add numbers

In this example, it shows how to overload a function to add numbers of different types: integers and decimals (double).

// Function to add two integers
public static int Add(int a, int b)
{
    return a + b; // Return the sum of two integers
}

// Function to add two decimal numbers (double)
public static double Add(double a, double b)
{
    return a + b; // Return the sum of two decimal numbers
}

// Use
int sumIntegers = Add(3, 5); // Call the function with two integers
double sumDecimals = Add(3.5, 5.7); // Call the function with two decimal numbers

// Print the results
Console.WriteLine($"Sum of integers: {sumIntegers}");
Console.WriteLine($"Sum of decimals: {sumDecimals}");

In this example, it shows how to overload a function to print employee information using different parameter sets.

// Function to print employee information using name and age
public static void PrintEmployee(string name, int age)
{
    Console.WriteLine($"Employee: {name}, Age: {age}"); // Print name and age
}

// Function to print employee information using name, age, and department
public static void PrintEmployee(string name, int age, string department)
{
    Console.WriteLine($"Employee: {name}, Age: {age}, Department: {department}"); // Print name, age, and department
}

// Use
PrintEmployee("John", 30); // Call the function with two parameters
PrintEmployee("Mary", 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. Normally there are better alternatives