Getting the name of an enum value at runtime in C#
Posted: (EET/GMT+2)
If you have an enum datatype in C#, you can access the numerical, internal values using convenient text based names. This makes better code than simply using constants or hard-coded numeric (int) values.
Sometimes, you want to convert a value to the string that you use to access the value in code. This can be done with an easy-to-use static method of the System.Enum class called GetValue. Here is an example on how to use this method:
public enum Colors { Red, Green, Blue, Yellow };
...
string colorName = Enum.GetName(typeof(Colors), 2)); // "Blue"
The above code also works correctly if you have set specific numeric values for the enumeration. So the solution for this question is pretty simple!