Method overloading in C Sharp (C#) - Time & Space Complexity
We want to understand how the time it takes to run overloaded methods changes as input size grows.
How does choosing different method versions affect the work done?
Analyze the time complexity of the following code snippet.
public class Calculator
{
public int Add(int a, int b) => a + b;
public int Add(int a, int b, int c) => a + b + c;
public int Add(int[] numbers)
{
int sum = 0;
foreach (var num in numbers)
sum += num;
return sum;
}
}
This code shows three methods named Add with different inputs: two numbers, three numbers, or an array of numbers.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop through the array in the third Add method.
- How many times: Once for each number in the input array.
When using the array version, the work grows as we add more numbers to sum.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows directly with the number of items to add.
Time Complexity: O(n)
This means the time to add numbers grows in a straight line with how many numbers you have.
[X] Wrong: "All overloaded methods take the same time regardless of input size."
[OK] Correct: The methods with fixed inputs run quickly, but the one that sums an array takes longer as the array grows.
Understanding how different method versions affect performance helps you explain your code choices clearly and confidently.
"What if the Add method used recursion to sum the array instead of a loop? How would the time complexity change?"
Practice
What is method overloading in C#?
Solution
Step 1: Understand method naming
Method overloading means using the same method name multiple times.Step 2: Check parameter differences
Each overloaded method must have different parameters (type or count).Final Answer:
Having multiple methods with the same name but different parameters -> Option CQuick Check:
Method overloading = Same name, different parameters [OK]
- Confusing overloading with overriding
- Thinking return type alone allows overloading
- Believing parameter names must differ
Which of the following is correct syntax for method overloading in C#?
public void Print(int x) { }
public void Print(string x) { }Solution
Step 1: Check method names
Both methods are named 'Print', which is allowed for overloading.Step 2: Check parameter types
One method takes an int, the other a string, so parameters differ.Final Answer:
Both methods have the same name but different parameter types -> Option AQuick Check:
Same name + different parameters = valid overloading [OK]
- Thinking parameter names affect overloading
- Believing return type difference is enough
- Assuming method names must be unique
What will be the output of this C# code?
class Program {
static void Display(int x) { Console.WriteLine("Int: " + x); }
static void Display(string x) { Console.WriteLine("String: " + x); }
static void Main() {
Display(5);
Display("Hello");
}
}Solution
Step 1: Identify which Display method is called
Display(5) calls Display(int x), printing "Int: 5".Step 2: Identify second method call
Display("Hello") calls Display(string x), printing "String: Hello".Final Answer:
Int: 5\nString: Hello -> Option BQuick Check:
Overloaded methods called by matching parameter type [OK]
- Assuming ambiguous call error
- Mixing output order
- Expecting runtime error
Find the error in this code snippet:
class Test {
void Show(int x) { }
void Show(int x) { }
}Solution
Step 1: Check method signatures
Both methods are named 'Show' and take one int parameter, so signatures are identical.Step 2: Understand overloading rules
Overloading requires different parameter types or counts; identical signatures cause errors.Final Answer:
Methods have the same signature causing a compile error -> Option DQuick Check:
Same name + same parameters = error [OK]
- Thinking return type difference matters
- Assuming parameter names affect overloading
- Believing code compiles fine
You want to create a class with a method Calculate that can add two integers, two doubles, or concatenate two strings. Which method signatures correctly implement method overloading?
Solution
Step 1: Check parameter types and counts
void Calculate(int a, int b), void Calculate(double a, double b), void Calculate(string a, string b) has three methods named Calculate with different parameter types but same count (2).Step 2: Verify overloading rules
Different parameter types allow overloading; all methods differ by parameter type.Final Answer:
void Calculate(int a, int b), void Calculate(double a, double b), void Calculate(string a, string b) -> Option AQuick Check:
Same name, different parameter types = valid overloading [OK]
- Using same parameters with different return types
- Changing parameter count incorrectly
- Confusing overloading with overriding
Ready to prove it?
Pass the test and this pattern moves unknown
