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 overloading in C#?
C# provides two techniques to implement static polymorphism −
- Function overloading
- Operator overloading
Function Overloading
Function overloading in C# allows multiple methods with the same name but different parameters within the same class. The compiler determines which method to call based on the number, types, and order of arguments passed.
Syntax
Following is the syntax for function overloading −
public returnType MethodName(parameter1) { }
public returnType MethodName(parameter1, parameter2) { }
public returnType MethodName(differentType parameter) { }
Function overloading can be achieved by changing −
- Number of parameters − Different count of arguments
- Data types of parameters − Different parameter types
- Order of parameters − Different sequence of parameter types
Example - Overloading by Number of Parameters
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
Example - Overloading by Parameter Types
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(2.5, 3.7));
Console.WriteLine("Result: " + Calculator.Add("Hello ", "World"));
}
}
The output of the above code is −
Adding integers Result: 15 Adding doubles Result: 6.2 Concatenating strings Result: Hello World
Operator Overloading
Operator overloading allows you to define custom behavior for operators when applied to user-defined types. Overloaded operators are implemented as static methods using the operator keyword followed by the operator symbol.
Syntax
Following is the syntax for operator overloading −
public static returnType operator +(Type operand1, Type operand2) {
// implementation
}
Overloadable and Non-Overloadable Operators
| Category | Operators | Overloadable |
|---|---|---|
| Unary | +, -, !, ~, ++, -- | Yes |
| Binary Arithmetic | +, -, *, /, % | Yes |
| Comparison | ==, !=, <, >, <=, >= | Yes (in pairs) |
| Logical | &&, || | No |
| Assignment | +=, -=, *=, /=, %= | No |
| Others | =, ., ?:, new, is, sizeof, typeof | No |
Example - Operator Overloading
using System;
public class Point {
public int X, Y;
public Point(int x, int y) {
X = x;
Y = y;
}
public static Point operator +(Point p1, Point p2) {
return new Point(p1.X + p2.X, p1.Y + p2.Y);
}
public static Point operator -(Point p1, Point p2) {
return new Point(p1.X - p2.X, p1.Y - p2.Y);
}
public override string ToString() {
return $"({X}, {Y})";
}
}
public class Program {
public static void Main() {
Point p1 = new Point(3, 4);
Point p2 = new Point(1, 2);
Point sum = p1 + p2;
Point diff = p1 - p2;
Console.WriteLine($"p1: {p1}");
Console.WriteLine($"p2: {p2}");
Console.WriteLine($"p1 + p2 = {sum}");
Console.WriteLine($"p1 - p2 = {diff}");
}
}
The output of the above code is −
p1: (3, 4) p2: (1, 2) p1 + p2 = (4, 6) p1 - p2 = (2, 2)
Conclusion
Overloading in C# enables static polymorphism through function overloading (same method name with different parameters) and operator overloading (custom behavior for operators on user-defined types). Both techniques allow more intuitive and flexible code by providing multiple ways to use the same method or operator name based on context.
