Alex Rivera | Logout

Using GetHashCode for getting Enum int value

Asked 2011-01-03T13:01:33.150
33

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 searching in a grid column.

To get the column index I am using

  MyEnumVariable.GetHashCode() 

Which works ok, or should I use

  (short)MyEnumVariable

I am a bit confused over using GetHashCode(). Is there any problem using that?

Edit
Report

1 Answer

6

Others have said why casting to int is better.

Another reason to not use GetHashCode is performance. It causes boxing. In my quick tests, GetHashCode was about 50 times slower (if it matters).

answered 2013-06-10T20:14:53.557

Your Answer