Operator Overload Problem
Unfortunately, interfaces cannot contain overloaded operators. Try injecting this into your compiler:
public interface IInequalityComaparable<T> { bool operator >(T lhs, T rhs); bool operator >=(T lhs, T rhs); bool operator <(T lhs, T rhs); bool operator <=(T lhs, T rhs); }
I don’t know why they didn’t allow this, but I assume that this complicates the definition of the language and it would be difficult to correctly implement the users.
Either this, or designers do not like the possibility of abuse. For example, imagine you are comparing >= on a class MagicMrMeow . Or even on class Matrix<T> . What does the result mean with respect to two values ?; Especially when there can be ambiguity?
Official work
Since the above interface is not legal, we have an IComparable<T> interface to get around the problem. It does not execute statements and provides only one method, int CompareTo(T other);
See http://msdn.microsoft.com/en-us/library/4d7sx9hd.aspx
The int result is actually three-bit or three-digit (similar to a Boolean , but with three states). This table explains the meaning of the results:
Value Meaning Less than zero This object is less than the object specified by the CompareTo method. Zero This object is equal to the method parameter. Greater than zero This object is greater than the method parameter.
Use workspace
To make the equivalent of value >= _minimumValue , you should write:
value.CompareTo(_minimumValue) >= 0
Merlyn Morgan-Graham Jun 25 2018-11-11T00: 00Z
source share