C# > Object-Oriented Programming (OOP) > Polymorphism > Compile-time Polymorphism (Method Overloading)
Method Overloading: Performing Arithmetic Operations
This example illustrates method overloading to perform addition operations on different data types (integers and doubles) using the same method name, `Add`. The `Calculator` class demonstrates how the C# compiler resolves which `Add` method to invoke based on the arguments supplied during the method call.
Core Concept: Compile-Time Binding
Compile-time polymorphism, also known as static polymorphism or early binding, occurs when the compiler determines which method to call based on the method signature (name and parameters) at compile time. Method overloading is a prime example of compile-time polymorphism. The decision is made before the program is executed, resulting in efficient execution.
C# Code Example
The code defines a `Calculator` class that includes two overloaded `Add` methods: one that accepts two integers and returns their sum as an integer, and another that accepts two doubles and returns their sum as a double. Then the method with int and double and double and int. In the `Main` method, we create a `Calculator` object and call the `Add` method with different data types. The compiler resolves the correct method to invoke based on the types of the arguments passed.
using System;
public class Calculator
{
public int Add(int a, int b)
{
return a + b;
}
public double Add(double a, double b)
{
return a + b;
}
public double Add(int a, double b)
{
return a + b;
}
public double Add(double a, int b)
{
return a + b;
}
public static void Main(string[] args)
{
Calculator calc = new Calculator();
int sumInt = calc.Add(5, 10);
Console.WriteLine("Sum of integers: " + sumInt);
double sumDouble = calc.Add(2.5, 7.5);
Console.WriteLine("Sum of doubles: " + sumDouble);
double sumIntDouble = calc.Add(5, 7.5);
Console.WriteLine("Sum of int and double: " + sumIntDouble);
double sumDoubleInt = calc.Add(2.5, 7);
Console.WriteLine("Sum of double and int: " + sumDoubleInt);
}
}
Real-World Example
Consider a `StringHelper` class with overloaded methods for substring extraction. One `Substring` method might take the starting index and length, while another might take the starting index and ending index. Libraries dealing with date and time calculations often employ overloading to handle different date/time formats and precision levels. Web API controllers can use method overloading based on the type and number of input parameters in POST requests to handle different data models.
Guidelines for Effective Overloading
Interview Preparedness
Be prepared to discuss the advantages and disadvantages of method overloading. Advantages include code reusability and improved readability, while potential disadvantages involve increased complexity and the risk of ambiguity. Explain the difference between overloading and overriding and the compile-time vs runtime resolution.
When to use them
Method overloading should be employed when you need to perform logically similar operations on data of different types or with a varying number of input parameters. It provides a flexible and intuitive way to handle different scenarios without creating distinct method names for each variation. Avoid using overloading when the underlying operations are fundamentally different, even if they share the same name.
Memory Implications
Method overloading has a negligible impact on memory footprint. The compiler stores metadata for each overloaded method in the class, but the overall size increase is minimal. The performance overhead is also insignificant since the method resolution occurs at compile time.
Alternative Approaches
Advantages
Disadvantages
FAQ
-
Can I overload a method with different return types if the parameter list is the same?
No, you cannot overload a method with different return types if the parameter list (number, type, and order of parameters) is the same. The return type is not part of the method signature. -
What happens if the compiler cannot determine which overloaded method to call?
If the compiler cannot determine which overloaded method to call due to ambiguity (e.g., two methods with parameter lists that are implicitly convertible to each other), it will generate a compile-time error indicating that the call is ambiguous.