是.NET HashSet的独特的计算完全基于Hash codeS?独特、HashSet、NET、Hash

2023-09-03 15:49:53 作者:不要活给别人看

我想知道是否有净的HashSet< T> 是哈希codeS或是否使用平等,以及完全基于

I was wondering whether the .Net HashSet<T> is based completely on hash codes or whether it uses equality as well?

我有我可能潜在实例数以百万计的实例特定类,并有一个合理的机会,一些散列coded会碰撞在这一点上。

I have a particular class that I may potentially instantiate millions of instances of and there is a reasonable chance that some hash codes will collide at that point.

我正在考虑使用的HashSet的存储这个类的一些实例,并想知道如果它实际上是值得做的事情 - 如果一个元素的唯一性只决定于它的散列code,那么这是没有用的我要真正的应用

I'm considering using HashSet's to store some instances of this class and am wondering if it's actually worth doing - if the uniqueness of an element is only determined on its hash code then that's of no use to me for real applications

MSDN文档似乎相当含糊关于这一主题 - 任何启示是pciated AP $ P $

MSDN documentation seems to be rather vague on this topic - any enlightenment would be appreciated

推荐答案

没有,它使用的平等,以及。根据定义,散列codeS不需要是唯一的 - 任何其中假定他们将被破碎。 的HashSet&LT; T&GT; 是明智的。它使用的IEqualityComparer<T> (默认为 EqualityComparer&LT; T&GT; .DEFAULT )。同时执行散列code代和平等的测试

No, it uses equality as well. By definition, hash codes don't need to be unique - anything which assumes they will be is broken. HashSet<T> is sensible. It uses an IEqualityComparer<T> (defaulting to EqualityComparer<T>.Default) to perform both hash code generation and equality tests.