After reading this question Why do "int" and "sbyte" GetHashCode functions generate different values? I wanted to dig further and found following behavior: sbyte i = 1; int j = 1; object.Equals(i, j) //false (1) object.Equals(j, i) //false (2) i.Equals(j) //false (3) j.Equals(i) //true (4) i == j //true (5) j == i //true (6) i.GetHashCode() == j.GetHashCode() //
读完这个问题之后为什么“int”和“sbyte”GetHashCode函数会生成不同的值? 我想进一步挖掘并发现以下行为: sbyte i = 1; int j = 1; object.Equals(i, j) //false (1) object.Equals(j, i) //false (2) i.Equals(j) //false (3) j.Equals(i) //true (4) i == j //true (5) j == i //true (6) i.GetHashCode() == j.GetHashCode() //false (7) (3)和(4)之间的区别打破了Equals应该是对称的要求。 (2)和(4
Is it possible to override the GetHashCode method for a string, int, int32 etc.. I dont want to create a new object or class and override it that away. I wonder if there was a way to override the type's method. Similar to an extension but not really. string myString = "sometext"; myString.GetHashCode(); -- I want to override this method. If you do not want to create your own class, the
是否有可能重写字符串,int,int32等的GetHashCode方法。 我不想创建一个新的对象或类并重写它。 我想知道是否有方法来重写类型的方法。 类似于扩展,但不是真的。 string myString = "sometext"; myString.GetHashCode(); -- I want to override this method. 如果您不想创建自己的类,那么不需要,因为只有在子类中才可以进行覆盖。 另一种方法是创建一个自定义的扩展方法,如: public static class MyExtensions {
Possible Duplicate: Why is it important to override GetHashCode when Equals method is overriden in C#? I dont implement the GetHashCode method of the Object class. so I get a number of warnings. Is there a way to check for equality where I just check the hash code in the Equals method, so implement both Equals and GetHashCode and not get the "Object.GetHashCode not implemented warning
可能重复: 当Equals方法在C#中被重写时,为什么重写GetHashCode非常重要? 我没有实现Object类的GetHashCode方法。 所以我得到了一些警告。 有没有一种方法来检查Equals方法中检查哈希码是否相等,因此实现Equals和GetHashCode并且没有得到“Object.GetHashCode未实现的警告?”。 如果我只是实现Equals而不实现GetHashCode会发生什么? myclass的实例可以在我的应用程序中更新。 public class MyClass{ private stri
I have a class that contains the following two properties: public int Id { get; private set; } public T[] Values { get; private set; } I have made it IEquatable<T> and overriden the object.Equals like this: public override bool Equals(object obj) { return Equals(obj as SimpleTableRow<T>); } public bool Equals(SimpleTableRow<T> other) { // Check for null if(
我有一个包含以下两个属性的类: public int Id { get; private set; } public T[] Values { get; private set; } 我已IEquatable<T>和重写所述object.Equals这样的: public override bool Equals(object obj) { return Equals(obj as SimpleTableRow<T>); } public bool Equals(SimpleTableRow<T> other) { // Check for null if(ReferenceEquals(other, null)) return false;
This question already has an answer here: What is the best algorithm for an overridden System.Object.GetHashCode? 17 answers Implementing GetHashCode correctly [duplicate] 1 answer 您应该将您的对象的哈希码计算基于不可变字段,如果您可以使Person的FirstName和LastName字段不可变,则可以使用基于Josh Bloch建议的以下模式: public override int GetHashCode() { int hash = 17; hash = hash * 31
这个问题在这里已经有了答案: 什么是重写的System.Object.GetHashCode的最佳算法? 17个答案 正确实现GetHashCode [复制] 1个答案 您应该将您的对象的哈希码计算基于不可变字段,如果您可以使Person的FirstName和LastName字段不可变,则可以使用基于Josh Bloch建议的以下模式: public override int GetHashCode() { int hash = 17; hash = hash * 31 + FirstName.GetHashCode(); hash = hash * 31 + LastName
This question already has an answer here: What is the best algorithm for an overridden System.Object.GetHashCode? 17 answers 您可以使用xor来包含所有相关属性的哈希码。 void Main() { var emp = new Employee { Id = 123, FirstName = "Billy", LastName = "Bobby", // lol, it's actually two first names }; int originalHash = emp.GetHashCode(); emp.FirstName = "
这个问题在这里已经有了答案: 什么是重写的System.Object.GetHashCode的最佳算法? 17个答案 您可以使用xor来包含所有相关属性的哈希码。 void Main() { var emp = new Employee { Id = 123, FirstName = "Billy", LastName = "Bobby", // lol, it's actually two first names }; int originalHash = emp.GetHashCode(); emp.FirstName = "Timmy"; Console.WriteLine ("Orig
This question already has an answer here: What is the best algorithm for an overridden System.Object.GetHashCode? 17 answers I couldn't say why they chose this particular hash code implementation, but with regard to this question: Why the method do right-shift (32-positions) first then do left-shift positons, Does it have specific meaning? The ShiftAndWrap() method here is a generic
这个问题在这里已经有了答案: 什么是重写的System.Object.GetHashCode的最佳算法? 17个答案 我不能说他们为什么选择这个特定的哈希代码实现,但对于这个问题: 为什么该方法首先进行右移(32位),然后进行左移位置,它有特定的含义吗? 这里的ShiftAndWrap()方法是一种算法的通用实现,用于将值左移N位,并将溢出包回到结尾。 所以在他们进行转换之前,他们首先得到最左边的N位,这样他们就可以将它们追加到最后。
This question already has an answer here: What is the best algorithm for an overridden System.Object.GetHashCode? 17 answers Always the latter. The former isn't sufficient because most bits are 0 (your numbers are most likely small), and those zeroes are in the most significant bits. You'd be wasting a lot of the hash code, thus getting a lot more collisions. Another common way o
这个问题在这里已经有了答案: 什么是重写的System.Object.GetHashCode的最佳算法? 17个答案 总是后者。 前者是不够的,因为大多数位是0(你的数字很可能很小),而这些零位是最重要的位。 你会浪费很多散列码,从而导致更多的冲突。 另一种常见的做法是将每个项目乘以一个素数并依靠溢出: return unchecked(FolderID.GetHashCode() * 23 * 23 + SubItemKind.GetHashCode() * 23
I'm using this function for a key in C#'s hash map like class, "Dictionary". x, y and z are Int16. public override int GetHashCode() { return (x << 16) | (UInt16)y; } How could I extend this to using all 3 variables? See What is the best algorithm for an overridden System.Object.GetHashCode? for the even more general case with any number of variables, of any typ
我在C#的哈希映射中使用了这个函数,就像类“Dictionary”一样。 x,y和z是Int16。 public override int GetHashCode() { return (x << 16) | (UInt16)y; } 我怎么能扩展这个使用所有3个变量? 查看重写的System.Object.GetHashCode的最佳算法是什么? 对于任何类型的任何数量的变量甚至更一般的情况。 对于任何类型的三个变量x,y,z,标准方法如下: return x.GetHashCode() ^ y.GetHashCode() ^ z.GetHashC
This question already has an answer here: What is the best algorithm for an overridden System.Object.GetHashCode? 17 answers A better choice for a hash could be something like this: public override int GetHashCode(byte[] obj) { int hash = 0; for (int i = 0; i < obj.Length; i++) { exponents = [0, 8, 16, 24]; exponent = exponents[i % 4]; unchecked
这个问题在这里已经有了答案: 什么是重写的System.Object.GetHashCode的最佳算法? 17个答案 散列的更好选择可能是这样的: public override int GetHashCode(byte[] obj) { int hash = 0; for (int i = 0; i < obj.Length; i++) { exponents = [0, 8, 16, 24]; exponent = exponents[i % 4]; unchecked { hash += obj[i] * (1 << i); } }