Let\'s say I have a type that implements IComparable.
I would have thought it\'s reasonable to expect that the operators ==
, !=
, >
Two main reasons:
Update as per Eric Lippert among others, the following is the appropriate standard implementation of the comparison operators in C# for a type UDT:
public int CompareTo(UDT x) { return CompareTo(this, x); }
public bool Equals(UDT x) { return CompareTo(this, x) == 0; }
public static bool operator < (UDT x, UDT y) { return CompareTo(x, y) < 0; }
public static bool operator > (UDT x, UDT y) { return CompareTo(x, y) > 0; }
public static bool operator <= (UDT x, UDT y) { return CompareTo(x, y) <= 0; }
public static bool operator >= (UDT x, UDT y) { return CompareTo(x, y) >= 0; }
public static bool operator == (UDT x, UDT y) { return CompareTo(x, y) == 0; }
public static bool operator != (UDT x, UDT y) { return CompareTo(x, y) != 0; }
public override bool Equals(object obj)
{
return (obj is UDT) && (CompareTo(this, (UDT)obj) == 0);
}
Just add the custom definition for private static int CompareTo(UDT x, UDT y)
and stir.