Subscribe to Subscribers
Talk to Webmaster Tony John

Online Members

baskar
More...


Resources » .NET programming » .NET Framework

Compile Time Polymorphism – Operator Overloading in C#


Posted Date:     Category: .NET Framework    
Author: Member Level: Gold    Points: 12



 


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





Did you like this resource? Share it with your friends and show your love!


Responses to "Compile Time Polymorphism – Operator Overloading in C#"
Feedbacks      

Post Comment:




  • 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:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Compile Time Polymorphism -- Function Overloading
    Previous Resource: Insertion , Updation, Deletion of records to Sql Server with ADO.NET
    Return to Resources
    Post New Resource
    Category: .NET Framework


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    Operators that can be overloaded  .  Operators that cannot be overloaded  .  Compile time polymorphism  .  Operator overloading  .  



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.