Saturday, July 28, 2018

c# - 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.


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


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...