i have written the following code
class Program
{
static void Main(string[] args)
{
Circle c1 = new Circle(5);
Circle c2 = new Circle(10);
Console.WriteLine(c1.Area().ToString());
if (c1>c2)
{
}
}
}
public class Circle:System.IComparable<Circle>,IComparable
{
public int radius { get;private set; }
public double Area()
{
return Math.PI * radius * radius;
}
public Circle(int radius)
{
this.radius = radius;
}
public int CompareTo(Circle c)
{
if (c.Area() == this.Area())
return 0;
if (c.Area() > this.Area())
return 1;
return -1;
}
public int CompareTo(Object c)
{
if (((Circle)c).Area() == this.Area())
return 0;
if (((Circle)c).Area() > this.Area())
return 1;
return -1;
}
}
However it the error
Error 1 Operator ‘>’ cannot be applied to operands of type ‘ConsoleApplication1.Circle’ and ‘ConsoleApplication1.Circle’
i have implemented both the methods and could not figure the error
Implementing IComparable does not by itself create the >, >=, <, <= operators for the class. If you want those operators to be usable with you class, you have to implement them:
If you decide to create these operators, you might also want to overload the .Equals method and the == operator so that your Circle object will in practise exhibit value type behavior under comparison operations.
Also, since the Area is directly proportional to the radius, you might to well to compare radiuses (? spelling) instead of areas.