简体   繁体   English

没有字段的对象:如何实现GetHashCode?

[英]Object with no fields: how to implement GetHashCode?

My objects in a complex structure have a property Dictionary<Object, Object> Annotations , where I can store meta and custom data used during some processing phases. 我的复杂结构中的Dictionary<Object, Object> Annotations具有一个Dictionary<Object, Object> Annotations属性,可以在其中存储在某些处理阶段使用的元数据和自定义数据。 I can now create a static readonly object UniqueName = new object() and use that as the key in the dictionary. 现在,我可以创建一个static readonly object UniqueName = new object()并将其用作字典中的键。 I use a static readonly object because I know for sure that it is unique. 我使用static readonly object因为我确定它是唯一的。 No one else can ever create the exact same object instance. 没有其他人可以创建完全相同的对象实例。 If instead I had used a string as a key, someone else could accidentally use the same string as the key and this could cause problems. 相反,如果我使用字符串作为键,那么其他人可能会意外地使用与该键相同的字符串,这可能会引起问题。

However, I understood from several sources (including here ) that the default GetHashCode implementation is based on the location of the object in memory. 但是,我从多个来源(包括here )了解到,默认的GetHashCode实现是基于对象在内存中的位置。 This may change when the object is moved by the garbage collector, and this would cause the hash it returns to change. 当对象由垃圾回收器移动时,这可能会更改,这将导致它返回的哈希值发生更改。 When that happens, the dictionary will be unable to find the value associated with the key object. 发生这种情况时,词典将无法找到与键对象关联的值。

How can I ensure that an object that has no fields never changes its hash code during its life time? 如何确保没有字段的对象在其生存期内从不更改其哈希码?

The naive solution is to create a Key object whose GetHashCode always returns 42 . 天真的解决方案是创建一个Key对象,该对象的GetHashCode始终返回42 However, this would severely impact the performance of the dictionary and is therefore not a solution. 但是,这将严重影响字典的性能,因此不是解决方案。

implementation may change the hash it returns 实现可能会更改它返回的哈希

The default hash code implementation will never change the value of a single object. 默认的哈希码实现将永远不会更改单个对象的值。 It will never change during the lifetime of that object and can therefore safely be used. 它在该对象的生存期内永远不会改变,因此可以安全地使用。 I (quickly) read those answers you're pointing at, but they don't talk about hash codes that change during the lifetime of a single object. 我(快速地)阅读了您所指向的答案,但是他们没有谈论在单个对象的生存期内更改的哈希码。

The default implementation of GetHashCode returns an index, rather than a memory address. GetHashCode的默认实现返回一个索引,而不是一个内存地址。 This index is unique for the lifetime of the object, so even if your object is moved around in memory it will always return the same value when you call GetHashCode 该索引在对象的生命周期中是唯一的,因此即使您的对象在内存中四处移动,当您调用GetHashCode时,它将始终返回相同的值。

However, once the object is garbage collected it valid for a new object you create afterwards to return the same value as a previous object did prior to garbage collection. 但是,一旦对象被垃圾回收,它对您之后创建的新对象有效,以返回与垃圾回收之前的先前对象相同的值。

In your example the UniqueName variable will always return the same value when you call GetHashCode , and no instance of Object that you create will ever return the same hash code for the lifetime of your program. 在您的示例中,当您调用GetHashCodeUniqueName变量将始终返回相同的值,并且您创建的Object实例将永远不会在程序的生命周期内返回相同的哈希码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM