Using GetHashCode for getting Enum int value
Using GetHashCode()
is incorrect. You should cast to int
. Using it the way you do is asking for raptors(or Raymond) to come and eat you.
That GetHashCode()
happens to return the integer value of the enum is an implementation detail and may change in future versions of .net.
GetHashCode()
guarantees that if two values are equal
their hash codes are equal too. The other way round is not guaranteed.
My rule of thumb is that if GetHashCode
were to return a constant value your program should still work correctly (but potentially be much slower) since a constant GetHashCode
trivially fulfills the contract, but has bad distribution properties.
When I did profiling, Enum.GetHashCode took a lot of cycles. I was able to improve it by using (int)Enum where possible.