You can redefine or overload most of the built-in operators available in C#. Thus a programmer can use operators with user-defined types as well. Overloaded operators are functions with special names the keyword operatorfollowed by the symbol for the operator being defined. similar to any other function, an overloaded operator has a return type and a parameter list.
For example, go through the following function −
The above function implements the addition operator (+) for a user-defined class Box. It adds the attributes of two Box objects and returns the resultant Box object.
When the above code is compiled and executed, it produces the following result −
When the above code is compiled and executed, it produces the following result −
csharp
Implementing the Operator Overloading
The following program shows the complete implementation −csharp
shellscript
Overloadable and Non-Overloadable Operators
The following table describes the overload ability of the operators in C# −| Sr.No. | Operators & Description |
|---|---|
| 1 | +, -, !, ~, ++, -- These unary operators take one operand and can be overloaded. |
| 2 | +, -, *, /, % These binary operators take one operand and can be overloaded. |
| 3 | ==, !=, <, >, <=, >= The comparison operators can be overloaded. |
| 4 | &&, || The conditional logical operators cannot be overloaded directly. |
| 5 | +=, -=, *=, /=, %= The assignment operators cannot be overloaded. |
| 6 | =, ., ?:, ->, new, is, sizeof, typeof These operators cannot be overloaded. |
Example
In the light of the above discussions, let us extend the preceding example, and overload few more operators −csharp
csharp

