B
B
BadCats2016-11-02 14:16:28
C++ / C#
BadCats, 2016-11-02 14:16:28

Equals and GetHashCode and overriding them?

They say there is a rule that if we redefine Equals, then we must redefine GetHashCode - why?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maa-Kut, 2016-11-02
@BadCats

First of all: to work correctly with hash collections (of the Dictionary type), namely, when used as a key. Their internal logic assumes that if for two objects Equalsreturns true, then the values ​​returned GetHashCodemust be equal. If this is not the case, then the collection will not work correctly: for example, it will not find values ​​by key, although they are there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question