Method Overloading in C#
What is Method Overloading?
Method overloading in C# allows a class to have multiple methods with the same name but different parameters (different type, number, or both). It enhances the readability of the program by allowing methods to perform similar tasks with different inputs.
Why Use Method Overloading?
Method overloading provides several benefits:
- Improves code readability and maintainability.
- Allows methods to handle different types or numbers of parameters.
- Enables the creation of more flexible and reusable code.
Example: Method Overloading in C#
Below is an example demonstrating method overloading. The Add
method is overloaded to handle different types and numbers of parameters.
Example Code:
public class Calculator
{
// Overloaded Add method: two integers
public int Add(int a, int b)
{
return a + b;
}
// Overloaded Add method: three integers
public int Add(int a, int b, int c)
{
return a + b + c;
}
// Overloaded Add method: two doubles
public double Add(double a, double b)
{
return a + b;
}
}
// Usage
Calculator calc = new Calculator();
int sum1 = calc.Add(5, 10); // Calls Add(int, int)
int sum2 = calc.Add(5, 10, 15); // Calls Add(int, int, int)
double sum3 = calc.Add(5.5, 10.5); // Calls Add(double, double)
Console.WriteLine($"Sum1: {sum1}"); // Output: Sum1: 15
Console.WriteLine($"Sum2: {sum2}"); // Output: Sum2: 30
Console.WriteLine($"Sum3: {sum3}"); // Output: Sum3: 16
In this example, the Calculator
class has three overloaded Add
methods:
Add(int a, int b)
: Adds two integers.Add(int a, int b, int c)
: Adds three integers.Add(double a, double b)
: Adds two double values.
Depending on the parameters passed, the appropriate Add
method is invoked.
Rules for Method Overloading
- Methods must have the same name.
- Methods must have different parameter lists (different type, number, or both).
- Return type alone is not sufficient to distinguish overloaded methods.
- Access modifiers can be different.
Key Characteristics of Method Overloading
- Enhances readability by allowing the same method name to perform different tasks.
- Facilitates code reuse without duplicating method names for similar operations.
- Resolved at compile time, providing better performance.
Method Overloading vs. Method Overriding
While both method overloading and method overriding allow methods to have the same name, they serve different purposes:
- Method Overloading: Same method name with different parameters within the same class.
- Method Overriding: Same method name and parameters in a derived class, redefining the base class method.
Key Points to Remember
- Method overloading allows multiple methods with the same name but different parameters.
- It improves code readability and maintainability by avoiding the need for different method names for similar actions.
- Overloaded methods must differ in their parameter lists; return type alone is insufficient.
- Method overloading is resolved at compile time, providing efficient execution.