Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding IEquatable

Tags:

c#

iequatable

When I implement objects that I want to compare using the IEquatable<T> interface:

  1. Why do I have to override Equals(object) method if I already implemented Equals(T)?
  2. Can I use == and != operators once I implement IEquatable<T>?
like image 360
leora Avatar asked Jan 04 '09 19:01

leora


People also ask

What is the point of IEquatable?

IEquatable<T> lets a structure implement a strongly typed Equals method so no boxing is required. Thus much better performance when using value types with generic collections. Reference types don't benefit as much but the IEquatable<T> implementation does let you avoid a cast from System.

How do I use IEquatable?

From the MSDN: The IEquatable(T) interface is used by generic collection objects such as Dictionary(TKey, TValue) , List(T) , and LinkedList(T) when testing for equality in such methods as Contains , IndexOf , LastIndexOf , and Remove .

Should I implement IEquatable?

A value type overriding Equals method indicates that it supports comparing two instances of the type for value equality. Consider implementing the IEquatable<T> interface to support strongly typed tests for equality. This ensures that callers performing equality checks invoke the strongly typed System.

Does INT implement IEquatable?

Just take the above example, int implements the IEquatable<int>. Likewise, the other primitive types also implement IEquatable<T>. Generally, IEquatable<T> is very useful for the value types.


2 Answers

  1. From MS Docs article on IEquatable<T>:

    If you implement IEquatable<T>, you should also override the base class implementations of Equals(Object) and GetHashCode() so that their behavior is consistent with that of the Equals(T) method. If you do override Equals(Object), your overridden implementation is also called in calls to the static Equals(Object, Object) method on your class. In addition, you should overload the op_Equality and op_Inequality operators. This ensures that all tests for equality return consistent results.

  2. No, operators do not use the Equals method. They must be overloaded separately to do so.

like image 127
Ray Booysen Avatar answered Oct 23 '22 04:10

Ray Booysen


1) As Ray said, override Equals(object) to ensure consistency when the method is called from classes which don't know (statically) that you implement IEquatable<T>. For instance, the non-generic collections classes will use Equals(object) for comparisons. You should also override GetHashCode().

2) Implementing IEquatable<T> doesn't overload the == and != operators automatically, but there's nothing to stop you from doing so, just like System.String does. You should document this very clearly if you do, however - and be careful when you make comparisons between other types of reference (e.g. MyType and Object) which will still use the identity comparison. I suspect it's not a great idea to do this unless it's going to be a very heavily used type in your code, where everyone will become very familiar with it and where the syntactic sugar of overloading == will really make a positive impact on readability.

like image 43
Jon Skeet Avatar answered Oct 23 '22 03:10

Jon Skeet