We have learned that the method can be overloaded in C#, in the same way, we can also overload the operator. We can perform the various operations with the same operator in C#.
Operator overloading means performing different operations with the same operator. We can overload most of the built-in operators that are provided by C#. We can also change the user-defined type as well such as structures or classes.
The keyword ‘operator‘ is used to overload an operator. Compile-time polymorphism is achieved through operator overloading.
Syntax:
1 2 3 4 | access_specifier class_name operator operator_symbol (argument_list) { // Code to be executed } |
The following table shows which operators present in C# can be overloaded and which are not.
Operators | Description |
---|---|
+, -, !, ~, ++, – – | unary operators take one operand and can be overloaded. |
+, -, *, /, % | Binary operators take two operands and can be overloaded. |
==, !=, = | Comparison operators can be overloaded. |
&&, || | Conditional logical operators cannot be overloaded directly |
+=, -+, *=, /=, %=, = | Assignment operators cannot be overloaded. |
Also, =, ., ?:, ->, new, is, sizeof, typeof
cannot be overloaded.
Overloading Unary Operators
The return type of unary operator overloading can be of any type except the void. The unary operator includes +, ~, ! and dot (.), these must have any return type but void but the return type must be the type of ‘Type’ for ++ and – operators and must be a bool type for true and false operators.
Also, remember that the true and false operators can be overloaded only as pairs. The compilation error occurs if a class declares one of these operators without declaring the other.
The syntax for overloading unary operator:
1 2 3 4 | public static classname operator operator_symbol (t) { // Code to execute } |
Let us go through an example in the C# program for overloading unary operators.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | using System; class Example { private int x; private int y; public Example() {} public Example(int i, int j) { x = i; y = j; } public void print() { Console.WriteLine("X = {0} Y = {1}", x, y); } public static Example operator-(Example eg) { eg.x = -eg.x; eg.y = -eg.y; return eg; } } class MainFunc { public static void Main() { Example obj1 = new Example(10, 20); obj1.print(); Example obj2 = new Example(); obj2 = -obj1; obj2.print(); } } |
Output:
X = 10 Y = 20
X = -10 Y = -20
Overloading Binary Operator
Binary operators are overloaded in pairs such as when an arithmetic operator is overloaded then the corresponding assignment operators also get overloaded automatically. For example, if we overload the +
operator then +=
also gets overloaded automatically.
The syntax for overloading binary operator:
we overload binary operator in the same manner as a unary operator, the only difference is the number of parameters here is two.
1 2 3 4 | public static classname operator operator_symbol (t1, t2) { // Code to be executed } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | using System; namespace BinaryOverloading { class Example { private int num1; private int num2; //constructor with no argument public Example() {} public Example(int i, int j) { num1 = i; num2 = j; } public void print() { Console.WriteLine("num1 = {0} num2 = {1}", num1, num2); } public static Example operator+(Example eg1, Example eg2) { Example temp = new Example(); temp.num1 = eg1.num1 + eg2.num1; temp.num2 = eg1.num2 + eg2.num2; return temp; } } class MainBinary { public static void Main() { Example obj1 = new Example(10, 20); obj1.print(); Example obj2 = new Example(20, 30); obj2.print(); Example obj3 = new Example(); obj3 = obj1 + obj2; obj3.print(); //final result } } } |
Output:
num1 = 20 num2 = 30
num1 = 10 num2 = 20
num1 = 30 num2 = 50