本文介绍了如何从一类原始的哈希代码重新实现GetHashCode?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

短的问题:如何才能获得一个物体的 object.GetHashCode()价值已重新实现的GetHashCode()

Short question: How do I get the object.GetHashCode() value for an object that has re-implemented GetHashCode()?

龙的故事:
所以我有大约十万的对象,每个共享很多(非编译时)常见的字符串。常见的如如果该值相等,这是同一个实例。

Long story:So I have about a hundred thousand objects, each sharing many (non-compile time) common strings. Common as in if the value is equal, it is the same instance.

知道了,我想我宁愿使用标准的对象比较(的ReferenceEquals ),而不是一个完整的字符串比较 - 尤其是当这些都抬起头来在字典上相当定期

Knowing that, I figure I'd rather use a standard object comparison (ReferenceEquals) rather than a full string compare - particularly as these are looked up in dictionaries on a fairly regular basis.

所以,我宣布一个类ReferenceEqualityComparer:的IEqualityComparer 与使用词典<字符串,TValue> ,认定它会是有反正有用和去试图实现这两种方法。

So I declare a class ReferenceEqualityComparer : IEqualityComparer to use with a Dictionary<string, TValue>, figuring it'd be useful to have anyway, and go about trying to implement the two methods.

等于是很容易的,使用 object.ReferenceEquals

Equals is easy enough, use object.ReferenceEquals.

不过,我如何获得 object.GetHashCode()的GetHashCode 方法?

But how do I get the equivalent of object.GetHashCode() for the GetHashCode method?

即如何获取对象实例的一些代表性?

ie how do I get some representation of an object's instance?

我知道有其他的方式,我可以去这样做 - 创建一个 InternedString 类持有到字符串的引用 ,但不实施等于的GetHashCode ,或存储索引,而不是每个对象的字符串,但我现在好奇 - 是否有真正实现一个通用的 ReferenceEqualityComparer

I know there are other ways I can go about doing this - create an InternedString class which holds a reference to the string, but does not implement Equals or GetHashCode, or store indexes rather than strings with each object, but I'm now curious - is there actually a way to implement a generic ReferenceEqualityComparer?

推荐答案

看一看。

这篇关于如何从一类原始的哈希代码重新实现GetHashCode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 01:01