Method Overloading
Method overloading means giving several methods the same name but different inputs, like draw that can draw a circle or a square depending on what you give it. C# picks the right one by looking at what you passed in.
In this page:
Syntax
static int MethodName(int a, int b) { }
static double MethodName(double a, double b) { } // same name, different parameters
static int MethodName(int a, int b, int c) { }
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
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
Common Mistakes
- Overloading only by return type, which is not allowed.
- Creating overloads that make a call ambiguous, such as
f(int, double)andf(double, int)called with two ints. - Assuming overload choice happens at runtime, when it is chosen at compile time from the argument types.
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: