Method Overloading is a key part of compile-time polymorphism, one of the two main types of polymorphism in C#.
It allows multiple methods to have the same name but different:
- Number of parameters
- Type of parameters
- Order of parameters
In this lesson, we explore method overloading with clear examples and best practices.
🌟 What is Method Overloading?
✔ Simple Definition:
Method Overloading = Same method name + different parameter list.
The compiler decides at compile time which method to call.
It improves:
- Readability
- Flexibility
- Developer convenience
💡 Why Do We Use Method Overloading?
✔ Makes APIs cleaner
✔ Avoids creating multiple confusing method names
✔ Allows different ways to perform the same operation
✔ Helps in real-world scenarios like Add(), Print(), Draw()
🧱 Basic Example — Add Method
public class Calculator
{
public int Add(int a, int b)
{
return a + b;
}
public double Add(double a, double b)
{
return a + b;
}
public int Add(int a, int b, int c)
{
return a + b + c;
}
}
Usage:
Calculator calc = new Calculator();
Console.WriteLine(calc.Add(2, 3));
Console.WriteLine(calc.Add(2.5, 3.5));
Console.WriteLine(calc.Add(5, 6, 7));
✔ Same name: Add
✔ Different parameters
✔ Compiler selects the correct method
🔄 Different Ways to Overload Methods
1️⃣ Changing Number of Parameters
void Print(string name) { }
void Print(string name, int age) { }
2️⃣ Changing Data Types of Parameters
void Draw(int radius) { }
void Draw(double radius) { }
3️⃣ Changing Order of Parameters
void Display(string name, int age) { }
void Display(int age, string name) { }
❌ What You Cannot Do
❌ Only changing return type is NOT enough:
int Test() { }
double Test() { } // ERROR
Return type does NOT matter for overloading.
Parameter list must be different.
🎨 Real-World Analogy
Example: “Book a Ticket”
You can book a ticket using:
- Name only
- Name + Age
- Name + Age + Passport Number
- Name + Age + Destination
Same action → bookTicket()
Different inputs → Overloading
🌈 Practical Example — Logger Class
public class Logger
{
public void Log(string message)
{
Console.WriteLine($"Info: {message}");
}
public void Log(string message, int errorCode)
{
Console.WriteLine($"Error {errorCode}: {message}");
}
public void Log(string message, bool isCritical)
{
if (isCritical)
Console.WriteLine($"CRITICAL: {message}");
else
Console.WriteLine($"Log: {message}");
}
}
🧠 Important Rules of Overloading
| Rule | Description |
|---|---|
| Must change parameter list | ✔ Required |
| Return type doesn’t matter | ✔ Cannot overload with return type alone |
| Access modifiers can differ | ✔ Allowed |
| Static and instance methods can be overloaded | ✔ Allowed |
| Params keyword can be overloaded | ✔ Allowed but avoid confusion |
🧰 Constructor Overloading
Overloading also applies to constructors.
public class Person
{
public Person() { }
public Person(string name) { }
public Person(string name, int age) { }
}
This helps initialize objects in different ways.
🔍 Overloading vs Overriding (Quick Difference)
| Feature | Overloading | Overriding |
|---|---|---|
| Type | Compile-time polymorphism | Runtime polymorphism |
| Same method name? | Yes | Yes |
| Same parameters? | No | Yes |
| Same class? | Yes | No |
| Keywords used | None | virtual, override |
📝 Mini Exercise
Create a class MathOperations with overloaded methods:
- Multiply(int, int)
- Multiply(double, double)
- Multiply(int, int, int)
Call each version and print results.
🔍 FAQs
Q1: Can we overload static methods?
Yes, static methods can be overloaded.
Q2: Can we overload methods by changing only the return type?
No, this will cause an error.
Q3: Can two methods have the same name and same parameters?
No, that causes a duplicate method error.
Q4: Is overloading faster than overriding?
Yes, because overloading is resolved at compile-time.
🎉 Conclusion
You now understand:
✔ What method overloading is
✔ Why it is used
✔ Different ways to overload methods
✔ What is not allowed
✔ The difference between overloading and overriding
✔ Practical and real-world examples
Method overloading is extremely powerful and makes your C# programs flexible and readable.