Creating a unique hash code based on some properties of an object

This question already has an answer here: What is the best algorithm for an overridden System.Object.GetHashCode? 17 answers You can try to do something like this : private int GetHashValue() { unchecked { int hash = 17; //dont forget nullity checks hash = hash * 23 + From.GetHashCode(); hash = hash * 23 + To.GetHashCode(); hash = hash * 23

根据对象的某些属性创建唯一的哈希码

这个问题在这里已经有了答案: 什么是重写的System.Object.GetHashCode的最佳算法? 17个答案 你可以尝试做这样的事情: private int GetHashValue() { unchecked { int hash = 17; //dont forget nullity checks hash = hash * 23 + From.GetHashCode(); hash = hash * 23 + To.GetHashCode(); hash = hash * 23 + Enable.GetHashCode(); return hash; } }

How to implement GetHashCode for a pair of 3D vectors

This question already has an answer here: What is the best algorithm for an overridden System.Object.GetHashCode? 17 answers Please reference this answer for a correct implementation of the GetHashCode(), as it's suitable for your question as well. As for your second part of the question, it should work fine as the bitwise xor (^) is commutative, meaning you'll get same result rega

如何为一对3D矢量实现GetHashCode

这个问题在这里已经有了答案: 什么是重写的System.Object.GetHashCode的最佳算法? 17个答案 请参考此答案以正确实施GetHashCode(),因为它也适合您的问题。 至于问题的第二部分,它应该正常工作,因为按位xor(^)是可交换的,这意味着无论操作顺序如何,您都会得到相同的结果。 然而,问题出现了,你只是想将你的向量放入HashMap中,或者你有一些不同的空间哈希方法吗?

C# GetHashCode() High Performance Hashing Algorithm

Possible Duplicate: What is the best algorithm for an overridden System.Object.GetHashCode? This is known to us that if we override the Equals method of Object in our custom types, we should also override and provide an implementation of GetHashCode method to support generating unique hash codes for use in support of Hashtable and Dictionary collection classes and may be other classes. This

C#GetHashCode()高性能散列算法

可能重复: 什么是重写的System.Object.GetHashCode的最佳算法? 我们知道,如果我们在自定义类型中重写Object的Equals方法,我们还应该覆盖并提供GetHashCode方法的实现,以支持生成唯一的哈希代码以用于支持Hashtable和Dictionary集合类,也可能是其他类。 这要求我们在被overriden内部使用的哈希算法的实现GetHashCode方法是最优和准确的,即它生成一个唯一的类型哈希,并尽可能快地提高性能,以提高使用我们类型的应用

How do you implement GetHashCode() on objects?

Duplicate: What is the best algorithm for an overridden System.Object.GetHashCode? If you've written an object with a variety of data-members, how do you intelligently implement GetHashCode()? One developer told me he just XORs (^ operator) the Hash of relevant data-fields, but I am unconvinced this is a "best-practices" implementation. If I had my way, there would be function

你如何在对象上实现GetHashCode()?

重复:重写的System.Object.GetHashCode的最佳算法是什么? 如果你写了一个包含各种数据成员的对象,你如何智能地实现GetHashCode()? 一位开发人员告诉我他只是XOR(^运算符)相关数据字段的散列,但我不相信这是一个“最佳实践”实现。 如果我有自己的方式,将会有函数Object.CombineHashes(Object [])和/或Object.CombineHashes(int [])来帮助智能地构建复杂对象的散列。 你会如何编写这些功能? 我通过将它们

Why is the xor operator used in computing hash code?

This question already has an answer here: What is the best algorithm for an overridden System.Object.GetHashCode? 17 answers What is C# exclusive or `^` usage? [closed] 7 answers The ^ operator is the bitwise exclusive-or operator. In this case it's being used as a convenient way to generate a hash code from three integers. (I don't think it's a very good way, but that'

为什么用于计算哈希代码的xor运算符?

这个问题在这里已经有了答案: 什么是重写的System.Object.GetHashCode的最佳算法? 17个答案 什么是C#独占或`^`用法? [已关闭] 7个回答 ^运算符是按位异或运算符。 在这种情况下,它被用作从三个整数生成散列码的便捷方式。 (我认为这不是一个好方法,但这是一个不同的问题......) 奇怪的是,在构造一个哈希码之后,他们再次使用了GetHashCode() ,这对于int来说是完全没有意义的,因为它只会返回int本身 - 所

Custom type GetHashCode

Possible Duplicate: What is the best algorithm for an overridden System.Object.GetHashCode? I need to override GetHashCode method for a type which consists of three strings. Here is my code: protected override int GetHashCode() { return str1.GetHashCode() + str2.GetHashCode() + str3.GetHashCode(); } What is a safe way of this method implementation? The best way is to avoid anything t

自定义类型GetHashCode

可能重复: 什么是重写的System.Object.GetHashCode的最佳算法? 我需要为由三个字符串组成的类型重写GetHashCode方法。 这是我的代码: protected override int GetHashCode() { return str1.GetHashCode() + str2.GetHashCode() + str3.GetHashCode(); } 这种方法实现的安全方式是什么? 最好的办法是避免任何会产生相同哈希码的东西,如果你: 交换操作数的顺序 有一个大部分为零的值,并且只是移动非零值

Implementing GetHashCode

Possible Duplicate: What is the best algorithm for an overridden System.Object.GetHashCode? What constitutes a good implementation of the GetHashCode method? I did some googling, and found some goodlines (MSDN) but it seems like the logic just manipulates two numbers stored as fields in the class. Is the actual logic this simple to implement this method? The minimum requirement is that th

实现GetHashCode

可能重复: 什么是重写的System.Object.GetHashCode的最佳算法? 什么构成GetHashCode方法的良好实现? 我做了一些Google搜索,并找到了一些goodlines(MSDN),但似乎逻辑只是操纵两个数字作为字段存储在类中。 这种方法的实际逻辑很简单吗? 最低要求是对于任何给定的值,哈希码应该是相同的。 所以,这个实现是有效的,但是这个分布是可怕的: public override int GetHashCode() { return 1; } 为了更好地工作

Implementing GetHashCode correctly

This question already has an answer here: What is the best algorithm for an overridden System.Object.GetHashCode? 17 answers Let's say your class looks like this: class Frob { public string Foo { get; set; } public int Bar { get; set; } public double FooBar { get; set; } } Let's say you define equals so that two instances of Frob are equal if their Foo and their Bar are

正确实现GetHashCode

这个问题在这里已经有了答案: 什么是重写的System.Object.GetHashCode的最佳算法? 17个答案 假设你的课堂是这样的: class Frob { public string Foo { get; set; } public int Bar { get; set; } public double FooBar { get; set; } } 比方说,你定义等于让两个实例Frob都是平等的,如果他们的Foo和他们的Bar是相等的,但FooBar无所谓。 然后你应该用Foo和Bar来定义GetHashCode 。 一种方式是这样的:

Overriding GetHashCode

This question already has an answer here: What is the best algorithm for an overridden System.Object.GetHashCode? 17 answers When you override GetHashCode() you also need to override Equals() , operator== and operator!= . And be very careful to meet all the requirements for those methods. The guidelines are here on MSDN. Most important quote: It is not a good idea to override operator

重写GetHashCode

这个问题在这里已经有了答案: 什么是重写的System.Object.GetHashCode的最佳算法? 17个答案 当你重写GetHashCode()你也需要重写Equals() , operator==和operator!= 。 并且要非常小心地满足这些方法的所有要求。 这些准则在MSDN上。 最重要的报价: 在非不可变类型中重写operator ==不是一个好主意。 如果你使用resharper,它可以为你生成GetHashCode(),Equals和operator方法体。 通过按Alt + Insert访问此

Using IEqualityComparer GetHashCode with a tolerance

I am trying to implement an IEqualityComparer that has a tolerance on a date comparison. I have also looked into this question. The problem is that I can't use a workaround because I am using the IEqualityComparer in a LINQ .GroupJoin() . I have tried a few implementations that allow for tolerance. I can get the Equals() to work because I have both objects but I can't figure out how t

使用带公差的IEqualityComparer GetHashCode

我试图实现一个IEqualityComparer ,它在日期比较上有一个容差。 我也研究过这个问题。 问题是我无法使用解决方法,因为我在LINQ .GroupJoin()使用了IEqualityComparer 。 我已经尝试了一些允许宽容的实现。 我可以让Equals()工作,因为我有两个对象,但我无法弄清楚如何实现GetHashCode() 。 我最好的尝试看起来像这样: public class ThingWithDateComparer : IEqualityComparer<IThingWithDate> { private re