Operator Overloading in C# programming
Operator Overloading in C# programming:
In the computer programming, operator overloading less commonly known as operator ad-hoc polymorphism. It is a specific case of polymorphism. In which some or all of operators like +,=,or== have different implementation depending upon the type of their arguments.
Operator Overloading in C#:
All unary and binary operation have pre-defined
implementations that are automatically available in any expressions. In
addition the pre-defined implementations, user defined implementations can also
be introduced in C#.
Remember that it is not possible to overload all operators
in C#. C# a special function called
operator function is used for overloading purpose. These special function or methods
must be public and static. They can take only value arguments.
Overload Unary Operator.
Overload binary Operator.
Unary Operator over loading in C#:
The general form of operator function for unary operator is
as follows:-
Public static return-type operator op(type t)
{
// user code here….
}
Where type must be a class or structure. The return type can
be any type including void.
Example of operator overloading in C#:
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 c1=new Complex();
C1.x=-c.x;
C1.y=-c.y;
Return temp;
}
}
Class myclass
{
Public static void main(String[] args)
{
Complex C2=new Complex();
C2.showXY();
}
}
Other Post Related to Asp.net and C#:
nice post
ReplyDeleteif you want to learn C programming language the read my blog
http://uniqueprogramminglko.blogspot.in/2015/08/tokens-in-c-language_19.html