Method Overloading allows multiple methods in the same class to have the same name but different parameters (type, number, or order).
class Calculator
{
// Add two integers
public int Add(int a, int b)
{
return a + b;
}
// Add three integers
public int Add(int a, int b, int c)
{
return a + b + c;
}
// Add two doubles
public double Add(double a, double b)
{
return a + b;
}
}
class Program
{
static void Main()
{
Calculator calc = new Calculator();
Console.WriteLine(calc.Add(5, 10)); // Output: 15
Console.WriteLine(calc.Add(5, 10, 15)); // Output: 30
Console.WriteLine(calc.Add(3.5, 2.5)); // Output: 6.0
}
}
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!