I have an enum
public enum INFLOW_SEARCH_ON
{
ON_ENTITY_HANDLE = 0,
ON_LABEL = 1,
ON_NODE_HANDLE = 2
} // enum INFLOW_SEARCH_ON
I have to use this enum for seraching in a grid columns
To get the column index I am using
MyEnumVariable.GetHashCode()
Which works ok, or should I use
(short)MyEnumVariable
I am bit confused over using GetHashCode(). Is there any problem using that.
You should use
(int)MyEnumVariable
for getting the literal value... you can also convert the other way like(INFLOW_SEARCH_ON)int
You also have the option methods provided by the Enum static class. For instance:
If all you are trying to do is get the value of the Enum. You can get the string ValueType, e.g. "ON_ENTITY_HANDLE" with
Enum.GetName(...)
.Otherwise, casting to (int) would definitely be preferred over
GetHashCode()
.Using
GetHashCode()
is incorrect. You should cast toint
. 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 areequal
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 constantGetHashCode
trivially fulfills the contract, but has bad distribution properties.You should use
(int) MyEnumVariable
. All enums by default are inherited fromint
.When I did profiling, Enum.GetHashCode took a lot of cycles. I was able to improve it by using (int)Enum where possible.
I see some comments that describe the disadvantages of using GetHashCode() for many reasons.
There are some cases where (int)INFLOW_SEARCH_ON.ON_LABEL doesn't work.
A good way to get the int value from Enum is using GeTTypeCode() method. This method works regardlesss of the underlying type. In this case it is int.
Output in this case is '1'.
Reference: Get int value from enum