Thuta Learning
IntermediateProgrammingbeginner

Method Overloading

Relax. We'll talk through this in plain words — no textbook voice.

Method Overloading lets you write several methods with the same name in one class, as long as they differ in the number or type of parameters. From the caller's side, you just remember one method name and it handles different inputs for you.

csharp
class Program
{
    static int Add(int x, int y)
    {
        return x + y;
    }

    static double Add(double x, double y)
    {
        return x + y;
    }

    static int Add(int x, int y, int z)
    {
        return x + y + z;
    }

    static void Main(string[] args)
    {
        Console.WriteLine(Add(8, 5));
        Console.WriteLine(Add(4.3, 6.2));
        Console.WriteLine(Add(1, 2, 3));
    }
}

How the compiler chooses

The compiler looks at the type and number of arguments you pass when calling the method, then picks the matching version. Add(8, 5) goes to the int version, while Add(4.3, 6.2) goes to the double version.

You should see
13 10.5 6
Method Overloading | Thuta Learning