CHASH Tutorial



C# METHOD OVERLOADING


C# Method Overloading

Method Overloading allows multiple methods in the same class to have the same name but different parameters (type, number, or order).

Why use Method Overloading?

  • Improves code readability.
  • Allows different ways to call a method with varying inputs.
  • Encapsulates similar operations in one method name.

Example of Method Overloading:

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
    }
}
  

Key Points:

  • Method name must be the same.
  • Parameter list must differ by type, number, or order.
  • Return type alone cannot differentiate overloaded methods.

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review