Operator overloading is a way to provide new implementations for C# operators, allowing them to be used with custom classes or structs.
Syntax:
public static return_type operator operator_symbol(parameters)
{
// Body of code
}
Here are a few important points to consider:
1. C# requires operator overloads to be defined as public static methods in the class. The static keyword means that the method belongs to the class itself and not to any particular object.
2. The operator keyword is followed by the operator you're overloading and the parameter list.
3. For binary operators, one of the operands must be the type of the class or struct that declares the operator.
Following is the list of independent overloadable operators:
Operator Name | Operator Symbol |
---|---|
Addition | + |
Subtraction | - |
Multiplication | * |
Division | / |
Modulus (remainder) | % |
Increment | ++ |
Decrement | -- |
Equality | == |
Inequality | != |
Greater than | > |
Less than | < |
Greater than or equal to | >= |
Less than or equal to | <= |
Bitwise AND | & |
Bitwise OR | | |
Bitwise XOR | ^ |
Bitwise Left Shift | << |
Bitwise Right Shift | >> |
There are two types of operator overloading in C#:
1. Overloading Unary Operators:
Unary operators operate on a single operand. They can be overloaded as member functions or non-member functions. When overloading unary operators as member functions, no additional argument is required. Here's an example:
using System;
class Counter
{
private int count;
public Counter(int initialCount = 0)
{
count = initialCount;
}
public static Counter operator -(Counter c)
{
return new Counter(-c.count);
}
public int GetCount()
{
return count;
}
}
class Program
{
static void Main()
{
Counter c1 = new Counter(5);
// Use the overloaded unary - operator
Counter c2 = -c1;
Console.WriteLine($"Original count: {c1.GetCount()}"); // Output: 5
Console.WriteLine($"Negated count: {c2.GetCount()}"); // Output: -5
Console.ReadLine();
}
}
In this example, the unary - operator is overloaded for the Counter class. It returns a new Counter object whose count field is the negation of the count field of the operand. So when you apply the - operator to a Counter object, you get a new Counter object with a negated count.
2. Overloading Binary Operators:
Binary operators operate on two operands. When overloading binary operators, one of the operands must be class object. Here's an example of overloading the addition (`+`) operator:
class Point
{
private int x, y;
public Point(int xVal, int yVal)
{
x = xVal;
y = yVal;
}
public static Point operator +(Point p1, Point p2)
{
return new Point(p1.x + p2.x, p1.y + p2.y);
}
}
class Program
{
static void Main()
{
Point p1 = new Point(2, 3);
Point p2 = new Point(4, 5);
Point p3 = p1 + p2;
// p3.x is 6, p3.y is 8
}
}
In the example above, the addition operator is overloaded as a member function of the `Point` class. It performs element-wise addition of the `x` and `y` coordinates of two `Point` objects and returns a new `Point` object.