简体   繁体   English

GetHashCode()与^

[英]GetHashCode() with ^

Does it have some special meaning when GetHashCode() function returns something using code which contains ^ symbol ? GetHashCode()函数使用contains ^ symbol代码返回内容时,它是否有一些特殊含义?

public class ClassProp
{
    public int Prop1 { get; set; }
    public int Prop2 { get; set; }
    public int Prop3 { get; set; }
    public int Prop4 { get; set; }
    public int Prop5 { get; set; }

    public override int GetHashCode()
    {
        return Prop1.GetHashCode() ^ Prop2.GetHashCode() ^ 
               Prop3.GetHashCode() ^ Prop4.GetHashCode() ^ Prop5.GetHashCode();
    }
}

^ is the C# XOR operator . ^是C# XOR运算符 Nothing "special" about it, just that the hash codes of all the class properties are XOR'd together. 没有什么“特别”的,只是所有类属性的哈希码都是异或的。

Edit : GetHashCode returns a generic code that is used as a shorthand identifier for a complex object. 编辑GetHashCode返回一个通用代码,用作复杂对象的速记标识符。 A common use is in hashing data structures when you want to store objects and then quickly retrieve them based on their hash code. 当您想要存储对象然后根据其哈希代码快速检索它们时,常见的用途是散列数据结构。 Assume a class Person and some objects with the corresponding hash codes: 假设一个Person类和一些具有相应哈希码的对象:

Alex 8540
John 9435
Peter 2453

These codes are generated based on some or all the fields of each object and must collide as rarely as possible to ensure efficient hashing. 这些代码是基于每个对象的一些或所有字段生成的,并且必须尽可能少地发生冲突以确保有效的散列。 Now we can store the objects in a hash table using the hash code: 现在我们可以使用哈希码将对象存储在哈希表中:

Entries
0 -> Alex
1 -> John
2 -> Peter

The objects are stored inside the table using their respective hash codes to determine the position. 使用各自的哈希码将对象存储在表内以确定位置。 Next they can be easily retrieved by using the same hash code. 接下来,可以使用相同的哈希代码轻松检索它们。

I suggest you find some literature about how hash tables work, because it's a bit too much to explain in an SO post. 我建议你找一些关于哈希表如何工作的文献,因为在SO帖子中解释有点太多了。

That's just the bitwise xor operator . 那只是按位xor运算符 It is often used to combine hash codes from different objects into a single overall hash code. 它通常用于将来自不同对象的哈希码组合成单个整体哈希码。

It's not one of the easiest things to search for on Google! 这不是Google最容易搜索的内容之一! My tip when searching for such things is to look at the table of all operators . 我在搜索这些东西时的提示是查看所有运算符表格

That's the bitwize XOR operator . 那是bitwize XOR运算符

This is a very common operator used when implementing GetHashCode . 这是实现GetHashCode时使用的非常常见的运算符。

That being said, in this case, that implementation may not be ideal. 话虽如此,在这种情况下,实施可能并不理想。 The problem with using XOR (alone) is that you're not necessarily reducing the chance of collisions. 使用XOR(单独)的问题在于,您不一定会减少碰撞的可能性。 The issue is that a class defined like so: 问题是这样定义的类如下:

class Foo
{
    public int Bar { get; set; }
    public int Baz { get; set; }

    // ...
    public override int GetHashCode()
    {  return this.Bar.GetHashCode() ^ this.Baz.GetHashCode(); }
}

Is going to create the same hash code when Bar==2 and Baz==4 as when Bar==4 and Baz==2. 当Bar == 2和Baz == 4时,将创建相同的哈希码,如Bar == 4和Baz == 2。 Depending on the use case, this may lead to more hash collisions, so it is something to be aware of when implementing GetHashCode. 根据用例,这可能会导致更多的哈希冲突,因此在实现GetHashCode时需要注意一些事项。 Also - you should be very careful when you make a mutable type like this that your hash code implementation matches your equality checks, etc. 此外 - 当您创建这样的可变类型时,您应该非常小心,以便您的哈希代码实现与您的相等性检查等相匹配。

The bitwise XOR operator works as follows: 按位XOR运算符的工作方式如下:

A = 10111 B = 01010 A = 10111 B = 01010

A ^ B = 11101 A ^ B = 11101

Different correspoding bits resutl in 1, similar ones result in 0. 不同的对应位在1中重新排列,类似的导致0。

In your case, those integers are being converted to binary first and then processed as in above example. 在您的情况下,这些整数首先被转换为二进制,然后如上例所示进行处理。

^ if the XOR operator in C# see here: http://msdn.microsoft.com/en-us/library/zkacc7k1.aspx ^如果C#中的XOR运算符在这里看到: http//msdn.microsoft.com/en-us/library/zkacc7k1.aspx

All your example is doing is XORing the hashcode from it's properties. 你所有的例子都是从它的属性中对哈希码进行异或。

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

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