Advertisement

Operator Overloading

By Bintu Chaudhary   Posted at  8:08:00 AM   C#

Home  »  C#  »  Operator Overloading


     All unary and binary operators have pre-defined semantic implementations, that are automatically available in any expressions. In addition to this pre-defined implementations, user defined implementations can also be introduced in C#. The mechanism of giving a special meaning to a standard C# operator with respect to a user defined data type such as classes or structures is known as Operator Overloading.

Contents

Overloadable Operators

     Operators can be overloaded to give them new additional semantic meaning. However, some of the C# operators cannot be overloaded at all. The following table shows the operators and their overloadability in C#.

Operators
Overloadability
+,-,*,/,%,&,|,<<,>> All C# binary operators can be overloaded.
+,-,!,~,++,--,true,false All C# unary operators can be overloaded.
==,!=,<,>,<==,>== All relational operators can be overloaded, but only as pairs.
&&,|| Can't be overloaded
(), (Conversion operator) Can't be overloaded
+=,-=,*=,/=,%= These compound assignment operators can be overloaded. But in C#, these operators are automatically overloaded when the respective binary operator is overloaded
=,.,?,->, new, is , as, sizeof
Can't be overloaded



Need for Operator Overloading

     Object oriented programming must exhibit polymorphism. Operator overloading is one such mechanism through which C# achieves polymorphism, though other mechanisms for polymorphism also exist.

Consider, for example , that the + operator is defined normally to add two numeric values irrespective of their type. In this sense + operator is overloaded by definition.
Thus,

int a = 20 + 30;
is a valid operation.

Moreover, if a programmer wishes to use the same operator + to mean adding (concatenation) two strings, the + operation must be overloaded accordingly. But for operator overloading feature it would not be possible to ascertain polymorphism in C#. By overloading + appropriatly, we can use + operator in the following manner.

String = "ABC" + "XYZ";



Defining Operator Overloading

     In C#, a special function called operator function is used for overloading purpose. These special fucntion or method must be public and static. They can take only value arguments. The ref and out parameters are not allowed as arguments to operator functions. The general form of an operator function is as follows.

public static return_type operator op (argument list)

Where the op is the operator to be overloaded and operator is the required keyword. For overloading the unary operators, there is only one argument and for overloading a binary operator there are two arguments. Remember that at least one of the arguments must be user-defined type such as class or struct type.

Overloading Unary Operators

     The general form of operator function for unary operators is as follows.

public static return_type operator op (Type t) { // Statements }

Where Type must be a class or struct. The return_type can be any type except void for unary operators like +,~,! and dot (.). but the return_type must be the type of Type for ++ and remember that the true and false operators can be overloaded only as pairs. The compliation errors occurs if a class declares one of these operators without declaring the other.

The following program overloads the unary (-) operator inside the Base class.



Output







Overloading Binary Operators

     An overloaded binary operator must take two arguments and at least one of them must be of the type class or struct, in which the operation in defined. But overloaded binary operators can return any value except the type void. The general form of an overloaded binary operator is as follows.

public static return_type operator op (Type t1, Type2 t2) { //Statement }

A concrete example is given below. This program overloads plus (+) operator to mean adding two complex numbers defined by class Base.

Output






Overloading Comparison Operators

     The binary operators such as ==,!=,< , > <=,>= can be overloaded only as pairs. Remember that when a binary arithmetic opeartor is overload, corresponding assignment operators also get overloaded automatically. For example if we overloaed + operator, it implicitly overloads the += operator also.





Rules of Operator Overloading

While Overloading an operator, the following rules must be kept in mind.
  1. The user defined operator declarations can't modify the syntax, precedence or associativity of an operator. For example, a + operator is always a binary operator having a predefined precedence and an associativity of left to right.

  2. User defined operator implementations are given preference over prefefined implementations.

  3. Operator overload methods can't return void.

  4. The operator overload methods can be overloaded just like any other methods in C#. The overloaded methods shoult differ in their type of arguments and/or number of arguments and/or order of arguments. Remember that in this case also the return type is not considered as part of the method signature.






About the Author

Nulla sagittis convallis arcu. Sed sed nunc. Curabitur consequat. Quisque metus enim, venenatis fermentum, mollis in, porta et, nibh. Duis vulputate elit in elit. Mauris dictum libero id justo.
View all posts by: BT9

Back to top ↑
Connect with Me