Mastering Method Overloading in C#: Concepts, Examples, and Best Practices
Mastering Method Overloading in C#
Discover how to overload methods in C#, including changing parameter counts, types, and order, with clear examples.
In C#, a class can contain multiple methods with the same name but distinct parameter lists—this is called method overloading. For example:
void display() { ... }
void display(int a) { ... }
float display(double a) { ... }
float display(int a, float b) { ... }
Here, the display() method is overloaded. These methods share the same name but accept different arguments.
Note: Overloading is determined solely by parameter lists; return types may differ.
1. By Changing the Number of Parameters
You can overload a method if the number of parameters differs.
void display(int a) {
...
}
...
void display(int a, int b) {
...
}
Example:
using System;
namespace MethodOverload {
class Program {
// method with one parameter
void display(int a) {
Console.WriteLine("Arguments: " + a);
}
// method with two parameters
void display(int a, int b) {
Console.WriteLine("Arguments: " + a + " and " + b);
}
static void Main(string[] args) {
Program p1 = new Program();
p1.display(100);
p1.display(100, 200);
Console.ReadLine();
}
}
}
Output
Arguments: 100 Arguments: 100 and 200
Based on the number of arguments passed, the appropriate overload is invoked.
p1.display(100)calls the single‑parameter version.p1.display(100, 200)calls the two‑parameter version.
2. By Changing the Data Types of Parameters
Overloading also works when the parameter types differ, even if the count is the same.
void display(int a) { ... }
...
void display(string b) { ... }
Example:
using System;
namespace MethodOverload {
class Program {
// method with int parameter
void display(int a) {
Console.WriteLine("int type: " + a);
}
// method with string parameter
void display(string b) {
Console.WriteLine("string type: " + b);
}
static void Main(string[] args) {
Program p1 = new Program();
p1.display(100);
p1.display("Programiz");
Console.ReadLine();
}
}
}
Output
int type: 100 string type: Programiz
p1.display(100)calls theintoverload.p1.display("Programiz")calls thestringoverload.
3. By Changing the Order of Parameters
When the parameter list has the same types and count, changing their order creates distinct overloads.
void display(int a, string b) { ... }
...
void display(string b, int a) { ... }
Example:
using System;
namespace MethodOverload {
class Program {
// method with int and string parameters
void display(int a, string b) {
Console.WriteLine("int: " + a);
Console.WriteLine("string: " + b);
}
// method with string and int parameters
void display(string b, int a) {
Console.WriteLine("string: " + b);
Console.WriteLine("int: " + a);
}
static void Main(string[] args) {
Program p1 = new Program();
p1.display(100, "Programming");
p1.display("Programiz", 400);
Console.ReadLine();
}
}
}
Output
int: 100 string: Programming string: Programiz int: 400
p1.display(100, "Programming")invokes theint, stringoverload.p1.display("Programiz", 400)invokes thestring, intoverload.
C Language
- C# Methods Explained: Declaration, Calling, Parameters, Return Types & More
- Mastering C# Abstract Classes & Methods: A Practical Guide
- Understanding C# Partial Classes and Methods
- Master sealed classes and methods in C#—learn when to use them, see clear examples, and boost code safety and performance.
- Mastering Constructor Overloading in C#
- C++ Function Overloading: A Practical Guide
- Master C++ Operator Overloading: Practical Examples & Best Practices
- C# Methods: Defining, Calling, and Using Functions
- Mastering Operator Overloading in C# for Custom Types
- C# Anonymous Methods: Simplifying Delegate Usage