Search⌘ K
AI Features

Method Overloading

Explore how to use method overloading in C# to create multiple methods with the same name but different parameters. Understand method signatures, apply the params keyword to accept variable arguments, and learn how parameter modifiers like ref and out affect method behavior.

We often require methods that execute similar logic but accept different parameter types or quantities. Consider a Multiply() method designed for two operands. If the requirements expand to support three operands, we use overloading to maintain a consistent API.

Method signature

In C#, we can create several methods that have the same name but differ in other portions of their signature. A method signature consists of several parts:

  • Method name

  • Parameter quantities

  • Parameter types

  • Parameter order ...

C# 14.0
namespace MethodOverloading;
public class MathOperation
{
public double Multiply(int firstNumber, int secondNumber)
{
return firstNumber * secondNumber;
}
public double Multiply(int firstNumber, int secondNumber, int thirdNumber)
{
return firstNumber * secondNumber * thirdNumber;
}
}
    ...