C# Operator Overloading Different Types

Operator Overloading in C#:

Operator overloading is one of the many exciting features of object-oriented programming. It means that C# operators can be defined to work with the user-defined data type such as structs and classes.

Defining Operator Overloading:

To define an additional task to an operator, we must specify what it means to the class to which the operator is applied. It is done with the help of a special method that is called the operator method. It has the following syntax:

public static retval operator op (arglist)
  Method of the body;

Where the operator is defined in much the same way as a method, except that we tell the compiler it is an operator we are defining by the operator keyword.

Features of Operator Overloading:

1. They must be defined as public and static.
2. The retval is a type of return value.
3. The arglist is the list of arguments passed. The number of arguments will be one for the unary operator and two for the binary operators.
4. In the case of unary operators, the argument must be the same type as that of the enclosing class or struct.
5. In the case of binary operators, the first argument must be of the same type as that of the enclosing class or struct and the second may be of any type.