C sharp - Method Overloading in C#
Definition:
Method overloading in C# means defining multiple methods in the same class with the same name but different parameters (type, number, or order).
Why Use Method Overloading?
-
Improves readability and code reusability.
-
Allows calling the same method in different ways based on input.
Example:
class Calculator { public int Add(int a, int b) { return a + b; } public double Add(double a, double b) { return a + b; } public int Add(int a, int b, int c) { return a + b + c; } }
Calculator calc = new Calculator(); Console.WriteLine(calc.Add(2, 3)); // Uses int version Console.WriteLine(calc.Add(2.5, 3.5)); // Uses double version Console.WriteLine(calc.Add(1, 2, 3)); // Uses 3-parameter version