GetHashCode() problem using xor

前端 未结 2 1126
萌比男神i
萌比男神i 2021-02-14 07:08

My understanding is that you\'re typically supposed to use xor with GetHashCode() to produce an int to identify your data by its value (as opposed to by its reference). Here\'s

2条回答
  •  北荒
    北荒 (楼主)
    2021-02-14 07:44

    First off - Do not implement Equals() only in terms of GetHashCode() - hashcodes will sometimes collide even when objects are not equal.

    The contract for GetHashCode() includes the following:

    • different hashcodes means that objects are definitely not equal
    • same hashcodes means objects might be equal (but possibly might not)

    Andrew Hare suggested I incorporate his answer:

    I would recommend that you read this solution (by our very own Jon Skeet, by the way) for a "better" way to calculate a hashcode.

    No, the above is relatively slow and doesn't help a lot. Some people use XOR (eg a ^ b ^ c) but I prefer the kind of method shown in Josh Bloch's "Effective Java":

    public override int GetHashCode()
    {
        int hash = 23;
        hash = hash*37 + craneCounterweightID;
        hash = hash*37 + trailerID;
        hash = hash*37 + craneConfigurationTypeCode.GetHashCode();
        return hash;
    }
    

    The 23 and 37 are arbitrary numbers which are co-prime.

    The benefit of the above over the XOR method is that if you have a type which has two values which are frequently the same, XORing those values will always give the same result (0) whereas the above will differentiate between them unless you're very unlucky.

    As mentioned in the above snippet, you might also want to look at Joshua Bloch's book, Effective Java, which contains a nice treatment of the subject (the hashcode discussion applies to .NET as well).

提交回复
热议问题