top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Define Operator Overloading in C# .net ?

+1 vote
300 views

Hai friends anybody say about the Operator Overloading in C#.net

posted Nov 19, 2014 by Balu

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

We had seen function overloading in the previous example. For operator Overloading , we will have look at the example below. We define a class rectangle with two operator overloading methods.

class Rectangle
{
private int Height;
private int Width;
public Rectangle(int w,int h)
{
Width=w;
Height=h;
} }
Public static bool operator >(Rectangle a, Rectangle b)
{
return a.Height > b.Height ;
}
public static bool operator <(Rectangle a,Rectangle b)
{
return a.Height < b.Height ;
} }

Let us call the operator overloaded functions from the method below. When first if condition is triggered, first overloaded function in the rectangle class will be triggered. When second if condition is triggered, second overloaded function in the rectangle class will be triggered.

public static void Main()
{
Rectangle obj1 =new Rectangle();
Rectangle obj2 =new Rectangle();
if(obj1 > obj2){
Console.WriteLine("Rectangle1 is greater than Rectangle2");
} 
if(obj1 < obj2)
{
Console.WriteLine("Rectangle1 is less than Rectangle2");
}}
answer Nov 19, 2014 by Manikandan J
...