C # How to choose a Hashcode for a class that violates the Equals contract?

I have several classes that for some reason do not follow the official contract Equals. When overwritten, GetHashCode()these classes simply return 0, so they can be used in a Hashmap.

Some of these classes implement the same interface, and Hashmaps use this interface as a key. Therefore, I decided that each class should at least return a different (but still constant) value in GetHashCode().

The question is how to choose this value. Should I just let the first class return 1, the next class 2, and so on? Or should I try something like

class SomeClass : SomeInterface {
    public overwrite int GetHashCode() {
        return "SomeClass".GetHashCode();
    }
}

why is the hash distributed more evenly? (Do I need to cache the return value myself or can the Microsoft compiler optimize this?)

Update: It is not possible to return an individual hash code for each object, since Equals violates the contract. In particular, I mean this problem .

+3
source share
4 answers

I ran into this exact problem when writing a vector class. I wanted to compare vectors for equality, but float operations give rounding errors, so I wanted approximate equality. In short, redefining peers is a bad idea if your implementation is not symmetrical, reflective, and transitive.

, equals , , , . , , , B.

- , . :

//Assume a == b, b == c, but a != c
var T = new Dictionary<YourType, int>()
T[a] = 0
T[c] = 1
return T[b] //0 or 1? who knows!

Set:

//Assume a == b, b == c, but a != c
var T = new HashSet<YourType>()
T.Add(a)
T.Add(c)
if (T.contains(b)) then T.remove(b)
//surely T can't contain b anymore! I sure hope no one breaks the properties of equality!
if (T.contains(b)) then throw new Exception()

, ApproxEquals. ==, , Equals.

-, , . IApproxEquals, GetApprox , , , . 3- , .

+1

, GetHashCode() . , "" GetHashCode() Object?

Edit

, , , , , , , , . , , - , , ( 101).

, , ... "" . . , .

+2

" Equals", , .

- , ... , Equals, , , -. , - .

- , ...

+2

-, HashTable/Dictionary Equals, , . - - .

You say that the Equals method was not implemented according to the contract. What do you mean? Depending on the type of violation, the HashTable or Dictionary will simply be slow (linear search) or not work at all.

+1
source

Source: https://habr.com/ru/post/1707869/


All Articles