Logical Pairs
It is quite common to overload the equals operator
(==) to test whether two objects are equal
(however equality might be defined for your object). C# insists that
if you overload the equals operator, you must also overload the
not-equals operator (!=). Similarly, the less-than
(<) and greater-than (>)
operators must be paired, as must the less-than or equals
(<=) and greater-than or equals
(>=) operators.
Tip
C
and
C++
programmers
take
note: Many C++
libraries require the implementation of only the
< operator or the =
operator, and other operations are derived from these. C# requires
the implementation of both halves of these paired operators.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access