适合小班授课的好哈希吗? (覆盖 GetHashCode)
我使用一些包含 1-2 个整数的身份类/结构,也可能是日期时间或小字符串。我用它们作为字典中的键。
对于这样的事情,对 GetHashCode 的一个好的重写是什么?事情很简单,但仍然有一定的性能希望。
谢谢
I use some identity classes/structs that contains 1-2 ints, maybe a datetime or a small string as well. I use these as keys in a dictionary.
What would be a good override of GetHashCode for something like this? Something quite simple but still somewhat performant hopefully.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看C# 基础。
它包含有关如何正确覆盖
GetHashCode()
的详细说明。摘自书中
正如这里已经提到的,您还必须考虑有关重写
Equals()
的一些要点,并且有一些代码示例展示了如何实现这两个函数。因此,这些信息应该提供一个起点,但我建议购买这本书并阅读完整的第 9 章(至少前十二部分),以获得有关如何正确实现这两个关键功能的所有要点。
Take a look into Essential C#.
It contains a detailed description on how to overwrite
GetHashCode()
correctly.Extract from the book
As already mentioned here you have also to consider some points about overriding
Equals()
and there are some code examples showing how to implement these two functions.So these informations should give a starting point but i recommend to buy the book and to read the complete chapter 9 (at least the first twelve sides) to get all the points on how to correctly implement these two crucial functions.