为什么我们需要 IEqualityComparer,IEqualityComparer界面?
'Equal' 和 'GetHashcode' 方法存在于对象类中,并且我们的类型继承了对象基类。 直接实现对象的两个方法和使用IComparer接口有什么区别?
如果我们覆盖对象的 Equal 和 GetHashCode ,并推送到哈希表,它将使用覆盖的 equal 方法?
使用 IEqualityComparer 构造函数新建哈希表有什么不同?
the 'Equal' and 'GetHashcode' method are exist in the object class, and our type inherit the object base class.
what's the different between implement the two methods of the object directly and using the IComparer interface?
if we overriding object's Equal and GetHashCode , and push to a hashtable , it will use the overring 's equal method?
what' the differents of new a hashtable with the IEqualityComparer constructor?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您需要能够对对象进行“排序”时,可以使用
IComparable
接口,它为您提供了一个方法 (CompareTo
) 来告诉您两个对象是否 <, =或> 。使用IEqualityComparer
的构造函数允许您给出特定的Equals
/GetHashCode
,该值可以与对象定义的不同。通常,Hashtable
将使用覆盖Equals
和GetHashCode
的对象(或基本object
Equals和
GetHashCode
)。举个例子,标准字符串以区分大小写的方式进行比较(
"A"
!="a"
),但您可以创建一个IEqualityComparer
帮助器类能够以不区分大小写的方式对字符串进行哈希处理。 (从技术上讲,此类已经存在于多个变体中:它们被称为StringComparer.InvariantCultureIgnoreCase
以及返回StringComparer
的StringComparer
的所有其他静态方法实现IComparer
、IEqualityComparer
、IComparer
的对象,IEqualityComparer
)请注意,
Hashtable
使用IEqualityComparer
可选参数,而不是通用版本IEqualityComparer< /code>,因为
Hashtable
是预泛型。The
IComparable
interface is used when you need to be able to "sort" objects, and it gives you a method (CompareTo
) that tells you if two objects are <, = or > . The constructor that usesIEqualityComparer
let you give a specificEquals
/GetHashCode
that can be different than the ones defined by your object. Normally theHashtable
will use your object overriddenEquals
andGetHashCode
(or the baseobject
Equals
andGetHashCode
).To make an example, the standard string compares in case sensitive way (
"A"
!="a"
), but you could make anIEqualityComparer
helper class to be able to hash your strings in a case insensitive way. (technically this class is already present in multiple variants: they are calledStringComparer.InvariantCultureIgnoreCase
and all the other static methods ofStringComparer
that return aStringComparer
object that implements theIComparer
,IEqualityComparer
,IComparer<string>
,IEqualityComparer<string>
)As a note, the
Hashtable
uses aIEqualityComparer
optional parameter, not the generic versionIEqualityComparer<T>
, becauseHashtable
is pre-generics.IComparer
接口(通用接口和非通用接口)允许您比较两个实例。Compare
方法允许您将对象本身与另一个实例进行比较。当然,当当前实例为 null 时,您将得到一个NullReferenceException
在这种情况下,因为您在“null”实例上调用Compare
。实现 IComparer 的类可以克服这个问题。因此,当您实现 IComparer 接口时,您将拥有一个具有“Compare”方法的类,可以像这样调用该方法:
这允许您执行以下操作:
当您实例化
Hashtable
时在您指定IEqualityComparer
实例的构造函数中,这意味着给定的IEqualityComparer
将用于确定哈希表中是否已存在某个键。否则,将使用键对象的 Compare 方法。
the
IComparer
interfaces (both the generic and the non-generic one) allow you to compare two instances with each other.The
Compare
method allows you to compare an object itself with another instance. Offcourse, when the current instance is null, you'll get aNullReferenceException
in this case, since you callCompare
on a 'null' instance. A class that implementsIComparer
can overcome this problem.So, when you implement the IComparer interface, you'll have a class which has a 'Compare' method, which can be called like this:
This allows you to do this:
When you instantiate a
Hashtable
with the constructor where you specify theIEqualityComparer
instance, this means that the givenIEqualityComparer
will be used to determine whether a certain key is already present in the Hashtable.Otherwise, the Compare method of the key-object will be used.