Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the default equality comparer for a set type?

Tags:

c#

In the MSDN API for the HashSet constructor with no arguments it states

Initializes a new instance of the HashSet class that is empty and uses the default equality comparer for the set type.

What is the default equality comparer for the set type, e.g. for a custom class?

BTW: Is it just me or is the MSDN API documentation really a bit thin on explanations? I stumble about such questions more than once when reading it.

like image 313
John Threepwood Avatar asked Jun 20 '13 22:06

John Threepwood


People also ask

What is the default Comparer C#?

IComparable interface, then the default comparer is the IComparable. CompareTo(Object) method of that interface. If type T doesn't implement either interface, then there is no default comparer, and a comparer or comparison delegate must be provided explicitly.

Should override equals C#?

With value types, you should always override the == operator. Like the Equals method, the default implementation of the == operator uses reflection and is slow. Use the same logic as the Equals method, and you'll get much better performance when you're doing equality comparisons on value types.

What collection would use an IEqualityComparer T to enforce uniqueness?

The Distinct extension method returns a new collection of unique elements from the given collection. The Distinct extension method doesn't compare values of complex type objects. You need to implement IEqualityComparer<T> interface in order to compare the values of complex types.

How do you compare objects in C#?

The most common way to compare objects in C# is to use the == operator. For predefined value types, the equality operator (==) returns true if the values of its operands are equal, false otherwise. For reference types other than string, == returns true if its two operands refer to the same object.


2 Answers

It means it will use the comparer returned by EqualityComparer<T>.Default for the element type T of the set.

As the documentation states:

The Default property checks whether type T implements the System.IEquatable interface and, if so, returns an EqualityComparer that uses that implementation. Otherwise, it returns an EqualityComparer that uses the overrides of Object.Equals and Object.GetHashCode provided by T.

So for your custom type, it will use the GetHashCode method you have defined to locate items in the set. If you have implemented IEquatable<T> it will use IEquatable<T>.Equals(T) for equality, otherwise it will use your Equals(object) method. This method defaults to reference equality as defined in the object class. Therefore if you are defining equality using either method, you should ensure you also override GetHashCode as well.

like image 94
Lee Avatar answered Oct 02 '22 21:10

Lee


By default, it will delegate to EqualityComparer<T>.Default. This returns a comparer that can compare two objects of type T.

For a custom class, this does a few things in this order:

  • if the class implements IEquatable<T>, it will delegate to the class's implementation of this interface
  • if the class has an Equals method defined, it will use that
  • as a last resort, it will use reference equality
like image 28
adrianbanks Avatar answered Oct 02 '22 21:10

adrianbanks