. Net – why do we need iequalitycomparer, iequalitycomparer interface?

The 'equal' and 'GetHashCode' methods exist in the object class, and our type inherits the object base class

If we rewrite the equal and GetHashCode of the object and push it to the hash table, it will use the equal method of overring?

What is the difference between the new hash table and the iequalitycomparer constructor?

Solution

When you need to be able to "sort" objects, use the IComparable interface, which provides you with a method (CompareTo), which tells you whether the two objects are <, = or > The constructor using iequalitycomparer allows you to provide specific equals / GetHashCode, which can be different from your object definition Typically, hashtable will override equals and GetHashCode (or the underlying objects equals and GetHashCode) with your object

For example, standard strings are compared in a case sensitive manner ("a"! = "a"), but you can enable the iequalitycomparer helper class to hash strings in a case insensitive manner (technically, there are many variants of this class: they are called stringcomparer.invariantcultureignorecase and all other static methods of stringcomparer. They return a stringcomparer object that implements IComparer, iequalitycomparer, IComparer < string >, iequalitycomparer < string >)

As a comment, hashtable uses the iequalitycomparer optional parameter instead of the generic version iequalitycomparer < T >, because hashtable is pre generic

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>