what is operator overloading in C#.Net?

Posted by Rajendra.prasad under C# on 5/28/2010 | Views : 35050 | Status : [Member] | Replies : 3
Hi All,
what is operator overloading in C#.Net?

Regards,
Rajendra




Responses

Posted by: Raja on: 5/28/2010 [Member] Starter

Up
0
Down
Operator overloading is a concept that enables us to redefine (do a special job) the existing operators so that they can be used on user defined types like classes and structs.

Look this link for more details
http://www.dotnetfunda.com/interview/exam455-what-is-operator-overloading-in-csharp-.aspx

Thank you

Regards,
Raja, USA

Rajendra.prasad, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Syedshakeer on: 5/31/2010 [Member] Starter

Up
0
Down
Hi,
While OverLoading a Operator always give public static access specifiers. 



Operator Overloading should return a CLS type and not void.


Atleast one operand to the operator must be of type UDC [User Defined Class] because we cannot overload a operator with two int operands and perform subtraction for + operator which C# does not allow.


Relational Operators are overloaded in Pairs only.ie if == is overloaded then So !=.


Same Operator can be overloaded with different function signature for eg.

check the following link for more information
http://www.c-sharpcorner.com/UploadFile/prasadh/OperatorOverloading11142005003229AM/OperatorOverloading.aspx

Syed Shakeer Hussain

Rajendra.prasad, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ksrao on: 6/14/2010 [Member] Starter

Up
0
Down
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.

All C # binary operators can be over loaded. i.e +, - , *, / , %,&,|, <<,>>.
All C# unray operators can be over loaded. i.e +,_,!,++,-- .
All relational operators can be over loaded , but only as pairs. i.e = =, !=, <>, <=, >=

In simple terms let us say if + is overloaded

if you want to find a + b
if a,b are integer result will be sum of a and b and result is int.
if a,b are flot result will be sum of a and b and result is flot.
if a,b are two strings result will be concatenation of String a and String b and final result is string.

In this way + is overloaded with different operations depending up on the data.

For more online free classes on c# click on http://www.wiziq.com/online-classes/c#-classes. .





Rajendra.prasad, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response