When I implement objects that I want to compare using the IEquatable<T>
interface:
Equals(object)
method if I already implemented Equals(T)
?==
and !=
operators once I implement IEquatable<T>
?From MS Docs article on IEquatable<T>
:
If you implement
IEquatable<T>
, you should also override the base class implementations ofEquals(Object)
andGetHashCode()
so that their behavior is consistent with that of theEquals(T)
method. If you do overrideEquals(Object)
, your overridden implementation is also called in calls to the staticEquals(Object, Object)
method on your class. In addition, you should overload theop_Equality
andop_Inequality
operators. This ensures that all tests for equality return consistent results.
No, operators do not use the Equals method. They must be overloaded separately to do so.