Click here to Skip to main content
15,912,082 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am using the following code snippets for overloading < operator

C#
Class example
{

static bool operator < (example a ,example b)
{
 /* code goes here*/
}
}

when i try to compile it,I get a error saying that
the operator < requires a matching operator > also to be defined.

How to over come this error
Posted

What it means is that if you declare a "less than" operator, you must also declare a "greater than" operator. Add this at a minimum:
C#
static bool operator > (example a ,example b)
{
 return !(b < a);
}
 
Share this answer
 
Comments
bsb25 21-Mar-12 8:25am    
Thanks..never knew that solution will be this easy...

Btw Can any one explain me why does C# wants both the operators to be declared??
OriginalGriff 21-Mar-12 8:40am    
AFAIK it isn't specified anywhere, but it is probably to do with optimisations: If both are defined, then the compiler is free to generate code using either as seems more appropriate at the time.
OriginalGriff 21-Mar-12 8:40am    
Forgot: the requirement is specifically stated here: http://msdn.microsoft.com/en-us/library/8edha89s(v=vs.71).aspx
Sergey Alexandrovich Kryukov 21-Mar-12 19:23pm    
Exactly. But the speculations on optimization is valid, I think, even though some stupid implementation could screw it up: imagine someone implemented < much slower than >, there is no a way a compiler could know it in advance...

Again, a 5 for the answer.
--SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900