How to get enum value in C#?

Posted by Poster under C# category on | Views : 27433
To get the enum value write following code

Code for enum
 public enum Priority { Low = 1, High = 2 }

Code to get enum value
int value = (int) Priority.Low;

This will set value = 1

If the value would have been string like "1", you can have casted to (string) instead of (int).
If you know the enum user friendly name and want to retrieve its value, you can use below code.
Priority myVal = (Priority)Enum.Parse(typeof(Priority), "high", true);

Above code snippet, shall print "2".

Hope this helps

Thank you.

Comments or Responses

Login to post response