Compile Time Polymorphism – Operator Overloading in C#

In my previous article (Compile Time Polymorphism - Operator Overloading) regarding compile time polymorphism I discussed all the possible scenarios of function overloading.

In this article we are going to discuss yet another aspect of compile time polymorphism which comes very handy when you want to create your own custom operator that perform some specialized functions.

Operator Overloading



Let’s understand by an example


struct MyOperatorOverloadedClass
{
public int x, y;
public MyOperatorOverloadedClass(int x, int y)
{
this.x = x;
this.y = y;
}

public static MyOperatorOverloadedClass operator +(MyOperatorOverloadedClass a, MyOperatorOverloadedClass b)
{
return new MyOperatorOverloadedClass(a.x * b.y + b.x * a.y, a.y * b.y);
}
}


So we have created a struct and overloaded the ‘+’ operator to perform some arithmetic operation and return values in a new struct.

Here goes the code sample:


MyOperatorOverloadedClass a = new MyOperatorOverloadedClass(1, 2);
MyOperatorOverloadedClass b = new MyOperatorOverloadedClass(3, 4);
MyOperatorOverloadedClass c = a + b; // c.x == 10, c.y == 8
Console.WriteLine("Value of x is {0} and that of y is {1}", c.x, c.y);


Output:-

Value of x is 10 and that of y is 8

As an example let’s have a look at few operator that are overloaded in .Net. This list is taken from MSDN.


Operator Name Type
, Comma Binary
! Logical NOT Unary
!= Inequality Binary
% Modulus Binary
%= Modulus assignment Binary
& Bitwise AND Binary
& Address-of Unary
&& Logical AND Binary
&= Bitwise AND assignment Binary
( ) Function call —
( ) Cast Operator Unary
* Multiplication Binary
* Pointer dereference Unary
*= Multiplication assignment Binary
+ Addition Binary
+ Unary Plus Unary
++ Increment1 Unary
+= Addition assignment Binary
– Subtraction Binary
– Unary negation Unary
–– Decrement1 Unary
–= Subtraction assignment Binary
–> Member selection Binary
–>* Pointer-to-member selection Binary
/ Division Binary
/= Division assignment Binary
< Less than Binary
<< Left shift Binary
<<= Left shift assignment Binary
<= Less than or equal to Binary
= Assignment Binary
== Equality Binary
> Greater than Binary
>= Greater than or equal to Binary
>> Right shift Binary
>>= Right shift assignment Binary
[ ] Array subscript —
^ Exclusive OR Binary
^= Exclusive OR assignment Binary
| Bitwise inclusive OR Binary
|= Bitwise inclusive OR assignment Binary
|| Logical OR Binary
~ One's complement Unary
delete Delete —
new New —
conversion operators conversion operators Unary

The following operators can’t be overloaded

Operator Name
. Member selection
.* Pointer-to-member selection
:: Scope resolution
? : Conditional
# Preprocessor convert to string
## Preprocessor concatenate


Comments



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: