Explain it simply
Method Overloading, in plain language
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.
Problem → Why → Solution
The problem — why this concept matters
Overloading one name for semantically different operations makes an API hard to predict.
A common mistake
Assuming one successful happy path proves the code is correct, maintainable, and production-ready.
Core concept — understand how it works
Overload resolution happens at compile time from the method name and parameter signatures. Return type alone cannot overload a method, and implicit conversions, optional parameters, and `params` can introduce ambiguity.
- typed input
- C# expression and method
- CLR value or object state
Explain the core concept and runtime behavior of Method Overloading
Core syntax or pattern
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));
}
}Overload resolution happens at compile time from the method name and parameter signatures. Return type alone cannot overload a method, and implicit conversions, optional parameters, and `params` can introduce ambiguity.
Read the code step by step
- typed input
class ProgramMethod 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.
- C# expression and method
static int Add(int x, int y, int z)Overload resolution happens at compile time from the method name and parameter signatures. Return type alone cannot overload a method, and implicit conversions, optional parameters, and `params` can introduce ambiguity.
- CLR value or object state
}Create area-calculation overloads and test integer and double arguments, named arguments, and an intentionally ambiguous candidate.
- typed input
- C# expression and method
- CLR value or object state
Success
Explain the core concept and runtime behavior of Method Overloading
Failure
Overloading one name for semantically different operations makes an API hard to predict.
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.
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.
13 10.5 6Real-world usage
How to apply it in a real project
Create area-calculation overloads and test integer and double arguments, named arguments, and an intentionally ambiguous candidate.
Mini challenge
Build it yourself
Create area-calculation overloads and test integer and double arguments, named arguments, and an intentionally ambiguous candidate.
- Rebuild the example without copying it
- Test a normal input and an edge case
- Predict the compiler or output result before running it
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));
}
}Expected: Explain the core concept and runtime behavior of Method Overloading
Show hint
Create area-calculation overloads and test integer and double arguments, named arguments, and an intentionally ambiguous candidate.
Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- typed input → C# expression and method → CLR value or object state
- Overload resolution happens at compile time from the method name and parameter signatures. Return type alone cannot overload a method, and implicit conversions, optional parameters, and `params` can introduce ambiguity.
- Overloading one name for semantically different operations makes an API hard to predict.
- Create area-calculation overloads and test integer and double arguments, named arguments, and an intentionally ambiguous candidate.
C# documentation — Microsoft Learn