Enum String Name from Value


I have an enum construct like this:

public enum EnumDisplayStatus
    None    = 1,
    Visible = 2,
    Hidden  = 3,
    MarkedForDeletion = 4

In my database, the enumerations are referenced by value. My question is, how can I turn the number representation of the enum back to the string name.

For example, given 2 the result should be Visible.

8/11/2019 4:29:23 PM

Accepted Answer

You can convert the int back to an enumeration member with a simple cast, and then call ToString():

int value = GetValueFromDb();
var enumDisplayStatus = (EnumDisplayStatus)value;
string stringValue = enumDisplayStatus.ToString();
8/11/2019 4:29:40 PM

If you need to get a string "Visible" without getting EnumDisplayStatus instance you can do this:

int dbValue = GetDBValue();
string stringValue = Enum.GetName(typeof(EnumDisplayStatus), dbValue);

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow