C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






Compile Time Polymorphism – Operator Overloading in C#


Posted Date: 20 Jun 2008    Resource Type: Articles    Category: .NET Framework

Posted By: shakti singh tanwar       Member Level: Diamond
Rating:     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





Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Operators that cannot be overloaded  .  Operators that can be overloaded  .  Operator overloading in c#  .  Operator overloading  .  Compile time polymorphism  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Compile Time Polymorphism -- Function Overloading
Previous Resource: Insertion , Updation, Deletion of records to Sql Server with ADO.NET
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

conference calls

Contact Us    Privacy Policy    Terms Of Use