简体   繁体   中英

GetHashCode() with ^

Does it have some special meaning when GetHashCode() function returns something using code which 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 . 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. 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:

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.

That's just the bitwise xor operator . 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! My tip when searching for such things is to look at the table of all operators .

That's the bitwize XOR operator .

This is a very common operator used when implementing 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. 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. Depending on the use case, this may lead to more hash collisions, so it is something to be aware of when implementing 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:

A = 10111 B = 01010

A ^ B = 11101

Different correspoding bits resutl in 1, similar ones result in 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

All your example is doing is XORing the hashcode from it's properties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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