C# Method Overloading
Method overloading means declaring several methods with the same name but different parameter lists. The compiler picks the right version based on the arguments you pass. You have used overloading from the very first lesson: Console.WriteLine has more than a dozen overloads — for string, int, double, bool and more.
A first example
Without overloading, you would need different names for the same idea: AddInts, AddDoubles, AddThree… With overloading they can all be called Add.
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 string Add(string a, string b) => a + b;
static void Main()
{
Console.WriteLine(Add(2, 3));
Console.WriteLine(Add(2.5, 3.25));
Console.WriteLine(Add(1, 2, 3));
Console.WriteLine(Add("over", "load"));
}
}5
5.75
6
overload
What makes overloads different?
Overloads must differ in their signature: the number of parameters, their types, their order, or their ref/out/in modifiers.
| Difference | Allowed as an overload? |
|---|---|
| Number of parameters | ✔ Area(int side) and Area(int w, int h) |
| Parameter types | ✔ Print(int x) and Print(string x) |
| Order of types | ✔ F(int a, string b) and F(string a, int b) |
| Only the return type | ✘ |
| Only the parameter names | ✘ |
Changing only the return type is not enough, because the compiler chooses an overload from the call — and a call like Parse("5"); does not say which return type it wants:
static int Parse(string s) { ... }
static double Parse(string s) { ... } // error CS0111: already defines a member called 'Parse' with the same parameter typesHow the compiler chooses
The compiler looks for the overload whose parameters match the argument types best. An exact match wins over one that needs an implicit conversion:
using System;
class Program
{
static void Show(int x) => Console.WriteLine(quot;int: {x}");
static void Show(long x) => Console.WriteLine(quot;long: {x}");
static void Show(double x) => Console.WriteLine(quot;double: {x}");
static void Show(object x) => Console.WriteLine(quot;object: {x}");
static void Main()
{
Show(42); // exact match: int
Show(42L); // exact match: long
Show(4.2); // exact match: double
Show(4.2f); // float -> double is the best conversion
Show('A'); // char -> int is the best conversion
Show("text"); // only object fits
}
}int: 42
long: 42
double: 4.2
double: 4.199999809265137
int: 65
object: text
Two surprises worth noting: a char argument picks the int overload (characters convert implicitly to their numeric code), and the float 4.2f becomes a slightly different double because float cannot store 4.2 exactly. If two overloads are equally good, the call is ambiguous and does not compile.
A practical example
Overloads are great for offering convenient variations of one operation. Often the simpler overloads just call the most complete one, so the real logic lives in a single place:
using System;
class Program
{
static string Repeat(char c, int count) => new string(c, count);
static string Repeat(string text, int count)
{
string result = "";
for (int i = 0; i < count; i++) result += text;
return result;
}
static string Repeat(string text, int count, string separator)
{
string[] parts = new string[count];
for (int i = 0; i < count; i++) parts[i] = text;
return string.Join(separator, parts);
}
static void Main()
{
Console.WriteLine(Repeat('*', 10));
Console.WriteLine(Repeat("ab", 3));
Console.WriteLine(Repeat("ha", 3, "-"));
}
}**********
ababab
ha-ha-ha
Overloading or optional parameters?
| Use overloading when… | Use optional parameters when… |
|---|---|
| The variants take different types | The variants differ only by default values |
| The implementations differ | One implementation handles all cases |
| You want separate documentation per variant | A single method is easier to read |
Tip: Keep overloads consistent: same name should mean same behaviour. An
Addoverload that secretly subtracts would be a nasty surprise.
Test yourself
Which pair of methods is a valid overload?
- int F(int x) and double F(int x)
- void F(int a) and void F(int b)
- void F(int x) and void F(string x)
- void F() and int F()
Given overloads M(int) and M(double), which one does M(5) call?
- M(int)
- M(double)
- Both
- It is ambiguous