Tuesday 15 January 2013

c# - What is the default equality comparer for a set type? -


In the MSDN API, for the constructor with no argument it says

A new instance Starts the hashaset class which is empty and compares the default similarity to the set type.

Comparison of default likeness for the set type, e.g. For a custom class?

BTW: Is it just me or is the MSDN API document actually a bit thin on clarification? When I read it I scream about more than one question.

This means that it will use the returned comparator, the element type of the set T < For / code>.

As the documentation:

The default property checks that the Type T system implements. The IEquatable interface and, if so, gives a similarity operator that uses the implementation otherwise, it gives an equity compaer that uses the override of the object. Acquires and Objects GetHashCode T.

, so for your custom type, it will use the GetHashCode method that you have defined to locate the item in the set . If you have IEquatable & lt; T & gt; is implemented, then it is Iquatable & lt; T & gt; .Equals (T) will use for parallelism, else it will use your similar () method to define the context in the context of the object Default is therefore, if you are defining equality by using any method, then you should ensure that you also override GetHashCode .

No comments:

Post a Comment