Revised answer (after clarification)
No, there is nothing cleaner than the cast. This is more informative than a method call, cheaper, shorter, etc. This is about as weak as you might hope.
Please note that if you want to write a general method for conversion, you will need to indicate what to convert it to: the enumeration can be based, for example, on byte or long . By casting, you will directly say what you want to convert to, and he just does it.
Original answer
What do you mean by "index" exactly? Do you mean a numerical value? Just hit int . If you mean "position in enum", you need to make sure that the values ββare in numerical order (as Enum.GetValues indicates - not the order of declaration), and then do:
public static int GetEnumMemberIndex<T>(T element) where T : struct { T[] values = (T[]) Enum.GetValues(typeof(T)); return Array.IndexOf(values, element); }
source share