重写 GetHashCode
如您所知,GetHashCode 返回一个半唯一值,可用于标识集合中的对象实例。作为一个好的做法,建议重写此方法并实现您自己的方法。
我的问题是 - 在处理自定义对象时您是否会覆盖此方法?如果是这样,您使用什么算法来生成唯一 ID?
我正在考虑生成一个 GUID,然后从该标识符获取整数数据。
As you know, GetHashCode returns a semi-unique value that can be used to identify an object instance in a collection. As a good practice, it is recommended to override this method and implement your own.
My question is - do you override this method when working on custom objects? If so, what algorithm do you use to generate the unique ID?
I was thinking about generating a GUID and then getting integer data from that identificator.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您使用 resharper,它可以为您生成 GetHashCode()、Equals 和运算符方法体。
按 Alt+插入 访问此菜单。
http://www.jetbrains.com/resharper/webhelp/Code_Generation__Equality_Members.html
If you use resharper it can generate the GetHashCode(), Equals and operator method bodies for you.
Access this menu by pressing Alt+Insert.
http://www.jetbrains.com/resharper/webhelp/Code_Generation__Equality_Members.html
当您重写
GetHashCode()
时,您还需要重写Equals()
、operator==
和operator!=
。并且要非常小心地满足这些方法的所有要求。这些指南位于 MSDN 上。最重要的引言:
When you override
GetHashCode()
you also need to overrideEquals()
,operator==
andoperator!=
. And be very careful to meet all the requirements for those methods.The guidelines are here on MSDN. Most important quote:
在我个人的使用中,我也只在重写 equals 方法时才进行重写。一般来说,我对我知道可能运行 LINQ to Objects 查询或其他比较操作的对象执行此操作。
如果是 LINQ to SQL 实体或 DTO 对象,我通常会返回主键值。无论您返回什么,如果不在本地存储该值,都可能会产生意外结果。
HTH。
In my personal usage, I only override when overriding equals method too. Generally, I do this for objects I know that I might run a LINQ to Objects query on, or some other comparison operation.
I usually return, if say a LINQ to SQL entity or DTO object, the primary key value. Whatever you return, if you don't store the value locally, it may produce an unexpected result.
HTH.
我通常会覆盖数据类(即值语义有意义的类)的哈希码和相等检查方法。看看这个问题以获得共同的实施。如果您确实覆盖哈希码,则覆盖等于。使用 GUID 是一个非常糟糕的想法,因为您希望两个不同实例但具有相同值的对象具有相同的哈希码,并且 equals 返回 true。
I would normally override hashcode and equality checking methods for data classes (i.e. classes where the value semantics makes sense). Have a look at this question for a common implementation. If you do override hashcode override equals. Using a GUID is a pretty terrible idea because you want two objects which are different instances but have the same value to have the same hashcode and for equals to return true.
如果您要覆盖 Equals,则只需覆盖 GetHashCode。默认的 GetHashCode 由运行时以与您想要的方式类似的方式实现 - 每个对象都有一个由运行时分配的隐藏字段。
如何覆盖 GetHashCode
实际上是你的IDE 应该为您执行此操作 - 当您键入“override GetHashCode”时,IDE 应该生成此样板代码。 Visual Studio 不会这样做,但 SharpDevelop 会这样做。
you only need to override GetHashCode if you are overriding Equals. The default GetHashCode is implemented by the runtime in a similar way you wanted to do it - every object has a hidden field assigned by the runtime.
How to override GetHashCode
Actually your IDE should do this for you - when you type "override GetHashCode" the IDE should generate this boilerplate code. Visual Studio does not do it but SharpDevelop does.
通常,我使用类的组件属性中聚合的 GetHashCode。例如
Generally I use the aggregated GetHashCode from the component properties of the class. E.g.