Method Overloading
In this page:
Method Overloading
Overloading lets several methods share the same name as long as their parameter lists differ in type or count. The compiler picks the right overload based on the arguments at the call site. Overloading is resolved entirely at compile time, not based on runtime values.
Note: Overloads should behave consistently — same name, conceptually the same operation, just different inputs.
Example: Method Overloading
using System;
class Program
{
static int Add(int a, int b) => a + b;
static double Add(double a, double b) => a + b;
static int Add(int a, int b, int c) => a + b + c;
static void Main(string[] args)
{
Console.WriteLine(Add(2, 3));
Console.WriteLine(Add(2.5, 3.5));
Console.WriteLine(Add(1, 2, 3));
}
}
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: