What is Method Overloading in C#?

πŸ’‘ Concept: Method Overloading

Method overloading in C# allows a class to have more than one method with the same name, but different parameters (type, number, or order). It enables compile-time polymorphism.

πŸ“˜ Quick Intro

Method overloading improves code readability and allows calling the same method in multiple ways. It occurs within the same class and is resolved at compile-time.

🧠 Analogy

Think of method overloading like a coffee machine button. Pressing "Coffee" gives different results based on cup size or coffee type β€” same name, different outcomes depending on input.

πŸ”§ Technical Explanation

  • Same method name, different parameter list (number, type, or order).
  • Return type is not sufficient for overloading alone.
  • Supports compile-time (static) polymorphism.
  • Can be combined with optional and default parameters.
  • Cannot overload by changing only the return type.

🎯 Use Cases

  • Allow flexibility in calling methods with different data types.
  • Provide multiple ways to initialize or compute values.
  • Improve code readability without needing multiple method names.
  • Useful in API design and libraries for overloading utility methods.

πŸ’» Code Example

public class Calculator
{
    public int Add(int a, int b) => a + b;

    public double Add(double a, double b) => a + b;

    public int Add(int a, int b, int c) => a + b + c;
}

// Usage
var calc = new Calculator();
Console.WriteLine(calc.Add(2, 3));         // Output: 5
Console.WriteLine(calc.Add(2.5, 3.2));     // Output: 5.7
Console.WriteLine(calc.Add(1, 2, 3));      // Output: 6

❓ Interview Q&A

Q1: What is method overloading?
A: Defining multiple methods with the same name but different parameter lists.

Q2: Can method overloading occur across multiple classes?
A: No, it occurs within the same class.

Q3: Is return type enough for overloading?
A: No, you must change parameter list.

Q4: What is compile-time polymorphism?
A: The method to call is determined at compile time β€” as with method overloading.

Q5: Can constructors be overloaded?
A: Yes, constructors support overloading based on parameters.

Q6: Can two overloaded methods have the same parameters in different order?
A: Yes, if their data types are different.

Q7: What’s the benefit of overloading?
A: Code reuse and readability with flexible parameter use.

Q8: Can default parameters interfere with overloading?
A: Yes, improper use may cause ambiguity.

Q9: Does C# support operator overloading?
A: Yes, but that’s a separate feature from method overloading.

Q10: Is overloading allowed in interfaces?
A: Yes, but only if parameter lists differ.

πŸ“ MCQs

Q1. What is method overloading?

  • Same name & return type
  • Same parameters
  • Same method name with different parameters
  • None of these

Q2. Which of these supports method overloading?

  • Return type
  • Variable names
  • Number of parameters
  • Class name

Q3. Can method overloading be based on return type?

  • Yes
  • No
  • Sometimes
  • Only in static methods

Q4. What kind of polymorphism does overloading support?

  • Run-time
  • Dynamic
  • Compile-time
  • Late-binding

Q5. Where must method overloading occur?

  • Derived class
  • Base class
  • Interface
  • Within same class

Q6. Is method name case-sensitive in overloading?

  • Yes
  • No
  • Only in VB.NET
  • Depends on OS

Q7. Which of these methods is overloaded?

  • Add()
  • Add(int)
  • Add(int, int), Add(int, int, int)
  • None

Q8. What if overloaded methods differ only by return type?

  • Compiler error
  • Runtime ambiguity
  • Accepted
  • Depends on CLR

Q9. Can static methods be overloaded?

  • No
  • Yes
  • Only in interfaces
  • Only in structs

Q10. Is overloading resolved at runtime?

  • Yes
  • No, at compile time
  • Sometimes
  • Depends on CLR

πŸ’‘ Bonus Insight

Method overloading simplifies APIs by letting developers use a single method name across scenarios. However, excessive overloading may reduce code clarity and introduce ambiguity with default parameters.

πŸ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

πŸ” Navigation

πŸ’¬ Feedback
πŸš€ Start Learning
Share:

Tags: