Enum String Name from Value [c#]


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);


I have an enum construct like this:

public enum EnumDisplayStatus

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.

how to get string value from Enum

You just need to call .ToString

 t.Status = Status.Success.ToString();

ToString() on Enum from MSDN

If you have enum Id passed, you can run:

t.Status = ((Status)enumId).ToString();

It casts integer to Enum value and then call ToString()

EDIT (better way): You can even change your method to:

public void CreateStatus(Status status , string userName)

and call it:


and cast to string:

t.Status = status.ToString();

Return the enumeration instead of the index

A fully working edit is live here: http://ideone.com/WFo4g


std::ostream& operator<<(std::ostream& os, const Status status)
    switch (status)
        case Idle: return os << "Idle";
        case Busy: return os << "Busy";
        default:   return os << "Status:" << status;

    return os << "<error>";

How can I get an enum name from the name of the enum type and an int

In addition to the name of enum itself you should know namespace and assembly that enum is declared at. Assuming you run code below in assembly where enum is defined and all your enums are defined in the same namespace you know at compile time, you can do it like that:

string dataType = "Cheese";
int dataValue = 3;
var enumType = Type.GetType("Namespaces.Of.Your.Enums." + dataType);
var name = Enum.GetName(enumType, dataValue);

If your enum is in another assembly than this code is running at - you will need to provide assembly qualified name of your enum type (again with namespace).

enum.Parse() would be your friend here.

Like so:

int cheeseType = 1;
(Cheese)Enum.Parse(typeof(Cheese), cheeseType.ToString());