Hash Codes for Immutable Types

Are there any considerations for immutable types regarding hash codes?

Do I have to generate it once, in the constructor?

How would you understand that the hash code has been fixed? Do I need to? If so, is it better to use the HashCode property instead of the GetHashCode method? Will there be a flaw in this? (Given that both will work, but the property will be recommended).

+5
source share
7 answers

Are there any considerations for immutable types regarding hash codes?

- ; - . , ; , -. ( , , , .)

, ?

; , ( ) . , , , . , , (1) , (2) .

, - ?

. -, . - . - , "" -, , - .

HashCode GetHashCode?

", GetHashCode(), , , - HashCode, "? ?

, .

+18

, , , .

, , ? , . "" , .

, .NET Java String - Java ,.NET - . , , , , (, -). , .NET .

+ . , , . , (, ) , .

, , , . , - - , , . GetHashCode().

+9

- , getHashCode , . , .

getHashCode , .

+4

, GetHashCode(), , -. - - , . , ( ) ?

- - . , , , .

, " - " .

+3

, .

- GetHashCode().

+2

, hashcode ? - , . , . GetHashCode, .

, , - , .

+1

, HashCode . , .

If profiling really shows that GethashCode is taking a significant amount of time, you might need to cache it as a fix.

But I would not consider this a part of ordinary practice.

+1
source

All Articles