top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration

Facebook Login
Site Registration

Define Operator Overloading in C# .net ?

+1 vote
59 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
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...