Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is method overloading in C#?
Method overloading in C# allows you to define multiple methods with the same name but different parameters. This enables you to create methods that perform similar operations but accept different types or numbers of arguments.
Method overloading can be achieved by changing the number of parameters, the data types of parameters, or the order of parameters. The compiler determines which method to call based on the arguments passed at runtime.
Syntax
Following is the syntax for method overloading −
public returnType MethodName(type1 param1) { }
public returnType MethodName(type1 param1, type2 param2) { }
public returnType MethodName(type2 param1, type1 param2) { }
Key Rules for Method Overloading
-
Methods must have the same name.
-
Methods must have different parameter lists (number, type, or order).
-
Return type alone cannot be used to overload methods.
-
Access modifiers can be different.
Using Method Overloading by Number of Parameters
Example
using System;
public class Demo {
public static int mulDisplay(int one, int two) {
return one * two;
}
public static int mulDisplay(int one, int two, int three) {
return one * two * three;
}
public static int mulDisplay(int one, int two, int three, int four) {
return one * two * three * four;
}
}
public class Program {
public static void Main() {
Console.WriteLine("Multiplication of two numbers: " + Demo.mulDisplay(10, 15));
Console.WriteLine("Multiplication of three numbers: " + Demo.mulDisplay(8, 13, 20));
Console.WriteLine("Multiplication of four numbers: " + Demo.mulDisplay(3, 7, 10, 7));
}
}
The output of the above code is −
Multiplication of two numbers: 150 Multiplication of three numbers: 2080 Multiplication of four numbers: 1470
Using Method Overloading by Data Type
Example
using System;
public class Calculator {
public static int Add(int a, int b) {
Console.WriteLine("Adding integers");
return a + b;
}
public static double Add(double a, double b) {
Console.WriteLine("Adding doubles");
return a + b;
}
public static string Add(string a, string b) {
Console.WriteLine("Concatenating strings");
return a + b;
}
}
public class Program {
public static void Main() {
Console.WriteLine("Result: " + Calculator.Add(5, 10));
Console.WriteLine("Result: " + Calculator.Add(5.5, 10.3));
Console.WriteLine("Result: " + Calculator.Add("Hello ", "World"));
}
}
The output of the above code is −
Adding integers Result: 15 Adding doubles Result: 15.8 Concatenating strings Result: Hello World
Using Method Overloading by Parameter Order
Example
using System;
public class Display {
public static void ShowInfo(int age, string name) {
Console.WriteLine("Age: " + age + ", Name: " + name);
}
public static void ShowInfo(string name, int age) {
Console.WriteLine("Name: " + name + ", Age: " + age);
}
}
public class Program {
public static void Main() {
Display.ShowInfo(25, "John");
Display.ShowInfo("Alice", 30);
}
}
The output of the above code is −
Age: 25, Name: John Name: Alice, Age: 30
Comparison of Method Overloading Types
| Type | Description | Example |
|---|---|---|
| By Number | Different number of parameters | Add(int a) vs Add(int a, int b) |
| By Data Type | Different parameter data types | Add(int a, int b) vs Add(double a, double b) |
| By Order | Different order of parameter types | Show(int, string) vs Show(string, int) |
Conclusion
Method overloading in C# provides compile-time polymorphism by allowing multiple methods with the same name but different parameter signatures. This feature enhances code readability and reusability by grouping related functionality under a single method name while accommodating different input scenarios.
