C # that takes an Enum element and returns an enum value (not an index)

Let's say I have the following declarations:

public enum Complexity { Low = 0, Normal = 1, Medium = 2, High = 3 } public enum Priority { Normal = 1, Medium = 2, High = 3, Urgent = 4 } 

and I want to encode it so that I get the value of the enum (and not the index, as I mentioned earlier):

 //should store the value of the Complexity enum member Normal, which is 1 int complexityValueToStore = EnumHelper.GetEnumMemberValue(Complexity.Normal); //should store the value 4 int priorityValueToStore = EnumHelper.GetEnumMemberValue(Priority.Urgent); 

What should this reusable feature look like?

TIA! -ren

+4
source share
6 answers

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); } 
+15
source

You can find the integer value of an enum by casting:

 int complexityValueToStore = (int)Complexity.Normal; 
+8
source

The most common way I know is to read the value__ field using reflection. This approach makes no assumptions about the base type of enum, so it will work with enums that are not based on Int32 .

 public static object GetValue(Enum e) { return e.GetType().GetField("value__").GetValue(e); } Debug.Assert(Equals(GetValue(DayOfWeek.Wednesday), 3)); //Int32 Debug.Assert(Equals(GetValue(AceFlags.InheritOnly), (byte) 8)); //Byte Debug.Assert(Equals(GetValue(IOControlCode.ReceiveAll), 2550136833L)); //Int64 

Note. I only tested this with the Microsoft C # Compiler. Shame does not seem to be a built-in way for this.

+4
source

I understand that this is not what you asked for, but it is something you can appreciate.

I found that you can find the integer value of an enumeration without translation if you know what the minimum value of an enumeration is:

 public enum Complexity { Low = 0, Normal = 1, Medium = 2, High = 3 } int valueOfHigh = Complexity.High - Complexity.Low; 

This will not work with priority unless you add a minimum value of 0 or add 1 back:

 public enum Priority { Normal = 1, Medium = 2, High = 3, Urgent = 4 } int valueOfUrgent = Priority.Urgent - Priority.Normal + 1; 

I find this technique much more aesthetically appealing than casting in int.

I'm not sure what will happen if you have an enumeration based on a byte or long - I suspect you will get a byte or long difference values.

+2
source

If you want to get the value, you can just translate enum to int. This would set the complexity of ValueToStore == 1 and priorityValueToStore == 4.

If you want to get the index ( i.e .: Priority.Urgent == 3), you can use Enum.GetValues , and then just find the index of the current current listing in this list. However, the listing order in the returned list may not be the same as in your code.

However, the second type option defeats the goal of Enum in the first place - you are trying to have discrete values ​​instead of lists and indexes. I would rethink your needs if that is what you want.

0
source

This is the easiest way to solve your problem:

 public static void GetEnumMemberValue<T>(T enumItem) where T : struct { return (int) Enum.Parse(typeof(T), enumItem.ToString()); } 

This works for me.

0
source

All Articles