In this article, we will discuss operator overloading in C#. It means that we are defining an operator for a class. It is very like method overloading. We can declare it with the help of the operator keyword like this:
Here we define an operator (/) with the operator keyword.
Declaration: We can generally declare it this way:
For Unary
For Binary
Example
First we look at an example of a unary operator(++):
The output will be
![Clipboard01.gif]()
Here we use the ++ operator so the values 10, 20, 30 will be 11, 21, 31.
Now we look at an example of a binary operator; here we do the calculations like (+,-,*,/) with the help of operator overloading.
Here we create a Class Calculation in our program like this:
Here we define the operators and a function (ShowTheResult()) to show the output. After that we write the following code in the Program class. By this the addition will be performed and the result will be shown with the help of The ShowTheResult() function.
Output
![2.gif]()
For subtraction, multiplication and division we write the following code:
Multiply
Division