Method Overloading in C#
What is Method Overloading?
Method Overloading is a feature in C# that allows a class to have multiple methods with the same name but with different parameters.
The overloaded methods can have different numbers of parameters, different parameter types, or different parameter arrangements. The compiler determines which method should be called based on the arguments passed to the method.
Add() methods.
One method may add two integers, another may add three integers,
and another may add two double values.
Why Use Method Overloading?
- Improves code readability.
- Allows the same logical operation to use one meaningful method name.
- Reduces the need for multiple differently named methods.
- Provides flexibility when working with different types or numbers of inputs.
- Supports compile-time polymorphism.
Rules of Method Overloading
To overload a method, the methods must have the same method name but differ in their parameter list.
- Different number of parameters.
- Different parameter data types.
- Different order of parameter types.
1. Overloading by Number of Parameters
We can create multiple methods with the same name but different numbers of parameters.
Example
using System;
class Calculator
{
static int Add(int a, int b)
{
return a + b;
}
static int Add(int a, int b, int c)
{
return a + b + c;
}
}
Console.WriteLine(Calculator.Add(10, 20));
Console.WriteLine(Calculator.Add(10, 20, 30));
30
60
2. Overloading by Parameter Type
Methods can also be overloaded by changing the data type of the parameters.
using System;
class Calculator
{
static int Add(int a, int b)
{
return a + b;
}
static double Add(double a, double b)
{
return a + b;
}
}
Console.WriteLine(Calculator.Add(10, 20));
Console.WriteLine(Calculator.Add(10.5, 20.5));
30
31
3. Overloading by Parameter Order
Methods can also be overloaded by changing the order of different parameter types.
using System;
class Display
{
static void Show(string name, int age)
{
Console.WriteLine($"Name: {name}, Age: {age}");
}
static void Show(int age, string name)
{
Console.WriteLine($"Age: {age}, Name: {name}");
}
}
Display.Show("Rahul", 25);
Display.Show(25, "Rahul");
Method Overloading and Compile-Time Polymorphism
Method overloading is also known as compile-time polymorphism. The compiler determines which overloaded method should be called during compilation based on the method arguments.
using System;
class Printer
{
static void Print(int number)
{
Console.WriteLine($"Number: {number}");
}
static void Print(string text)
{
Console.WriteLine($"Text: {text}");
}
}
Printer.Print(100);
Printer.Print("Hello");
Can We Overload Only by Return Type?
No. C# does not allow method overloading based only on the return type. The parameter list must be different.
The following example is invalid:
// Invalid
static int Calculate(int a)
{
return a;
}
static double Calculate(int a)
{
return a;
}
Constructor Overloading
Method overloading is not limited to normal methods. Constructors can also be overloaded by providing different parameter lists.
using System;
class Student
{
public string Name;
public int Age;
public Student()
{
Name = "Unknown";
Age = 0;
}
public Student(string name)
{
Name = name;
}
public Student(string name, int age)
{
Name = name;
Age = age;
}
}
Student student1 = new Student();
Student student2 = new Student("Rahul");
Student student3 = new Student("Priya", 22);
Example 🌍🤓
Method overloading is commonly used in real-world applications. For example, a payment system may process payments using different input information, while a logging system may accept different types of messages.
using System;
class Payment
{
public void ProcessPayment(double amount)
{
Console.WriteLine($"Processing payment of ₹{amount}");
}
public void ProcessPayment(double amount, string cardNumber)
{
Console.WriteLine(
$"Processing ₹{amount} using card {cardNumber}");
}
public void ProcessPayment(double amount, string cardNumber, string currency)
{
Console.WriteLine(
$"Processing {currency} {amount} using card {cardNumber}");
}
}
Payment payment = new Payment();
payment.ProcessPayment(1000);
payment.ProcessPayment(1000, "XXXX-1234");
payment.ProcessPayment(1000, "XXXX-1234", "INR");
Method Overloading vs Method Overriding
| Feature | Method Overloading | Method Overriding |
|---|---|---|
| Purpose | Same method name with different parameters | Change inherited method behavior |
| Polymorphism | Compile-time | Run-time |
| Inheritance Required? | No | Yes |
| Parameters | Must be different | Usually same signature |
Best Practices
- Use overloading when methods perform the same logical operation.
- Keep overloaded methods easy to understand.
- Avoid creating too many overloaded versions unnecessarily.
- Use meaningful parameter names.
- Keep the behavior of overloaded methods logically consistent.
Common Mistakes to Avoid
- Do not try to overload methods only by changing the return type.
- Make sure overloaded parameter lists are actually different.
- Avoid confusing method names when the operations are unrelated.
- Remember that the compiler selects the overloaded method based on the arguments supplied.
Summary
Method Overloading allows multiple methods in the same class to have the same name with different parameter lists. Methods can be overloaded by changing the number, type, or order of parameters. Method overloading provides compile-time polymorphism and makes code more readable and flexible. However, changing only the return type cannot overload a method.
Practice Questions
-
Create an
Add()method overloaded for two and three integers. - Create overloaded methods to calculate the area of a circle and rectangle.
-
Create overloaded
Display()methods for string and integer values. - Create a class with three overloaded constructors.
- Explain why methods cannot be overloaded only by return type.
- Explain the difference between method overloading and method overriding.