All unary and binary operators havepre-defined implementations, that are automatically available in anyexpressions. In addition to this pre-defined implementations, userdefined implementations can also be introduced in C#. The mechanism ofgiving a special meaning to a standard C# operator with respect to auser defined data type such as classes or structures is known asoperator overloading. Remember that it is not possible to overload alloperators in C#. The following table shows the operators and theiroverloadability 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.
&&, || They can't be overloaded
() (Conversion operator) They 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 These operators can't be overloaded
In C#, a special function called operatorfunction is used for overloading purpose. These special function ormethod must be public and static. They can take only value arguments.The ref and out parameters are not allowed as arguments to operatorfunctions. 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 therequired keyword. For overloading the unary operators, there is onlyone argument and for overloading a binary operator there are twoarguments. Remember that at least one of the arguments must be auser-defined type such as class or struct type.
Overloading Unary OperatorsThe general form of operator function for unary operators is asfollows.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 o remember that the true and false operators can be overloaded onlyas pairs. The compilation error occurs if a class declares one of theseoperators without declaring the other.
The following program overloads the unary – operator inside the class Complex
// Unary operator overloading
// Author: rajeshvs@msn.com
using System;
class Complex
{
private int x;
private int y;
public Complex()
{
}
public Complex(int i, int j)
{
x = i;
y = j;
}
public void ShowXY()
{
Console.WriteLine(\"{0} {1}\",x,y);
}
public static Complex operator -(Complex c)
{
Complex temp = new Complex();
temp.x = -c.x;
temp.y = -c.y;
return temp;
}
}
class MyClient
{
public static void Main()
{
Complex c1 = new Complex(10,20);
c1.ShowXY(); // displays 10 & 20
Complex c2 = new Complex();
c2.ShowXY(); // displays 0 & 0
c2 = -c1;
c2.ShowXY(); // diapls -10 & -20
}
}
Overloading Binary Operators
An overloaded binary operator must take twoarguments, at least one of them must be of the type class or struct, inwhich the operation is defined. But overloaded binary operators canreturn any value except the type void. The general form of a overloadedbinary operator is as follows.
public static return_type operator op (Type1 t1, Type2 t2)
{
//Statements
}
A concrete example is given below
// binary operator overloading
// Author: rajeshvs@msn.com
using System;
class Complex
{
private int x;
private int y;
public Complex()
{
}
public Complex(int i, int j)
{
x = i;
y = j;
}
public void ShowXY()
{
Console.WriteLine(\"{0} {1}\",x,y);
}
public static Complex operator +(Complex c1,Complex c2)
{
Complex temp = new Complex();
temp.x = c1.x+c2.x;
temp.y = c1.y+c2.y;
return temp;
}
}
class MyClient
{
public static void Main()
{
Complex c1 = new Complex(10,20);
c1.ShowXY(); // displays 10 & 20
Complex c2 = new Complex(20,30);
c2.ShowXY(); // displays 20 & 30
Complex c3 = new Complex();
c3 = c1 + c2;
c3.ShowXY(); // dislplays 30 & 50
}
}
The binary operators such as = =, ! =, <,>, < =, > = can be overloaded only as pairs. Remember thatwhen a binary arithmetic operator is overloaded, correspondingassignment operators also get overloaded automatically. For example ifwe overload + operator, it implicitly overloads the + = operator also.
Summary
1.The user defined operator declarations can'tmodify the syntax, precedence or associativity of an operator. Forexample, a + operator is always a binary operator having a predefinedprecedence and an associativity of left to right.
2.User defined operator implementations are given preference over predefined implementations.
3.Operator overload methods can't return void.
4.The operator overload methods can be overloaded just like anyother methods in C#. The overloaded methods should differ in their typeof arguments and/or number of arguments and/or order of arguments.Remember that in this case also the return type is not considered aspart of the method signature.
Conclusion
The operator overloading is one of the keyconcepts of C# and are very interesting aspects of the language. I'vegiven you enough information to use operator overloading in your codeand shown you some examples. The feedback is always welcome. Feel freeto contact me at rajeshvs@msn.com

