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
.
Get the value of an Enum To get the value of enum we can simply typecast it to its type. In the first example, the default type is int so we have to typecast it to int. Also, we can get the string value of that enum by using the ToString() method as below.
We can convert an enum to string by calling the ToString() method of an Enum.
Since C# doesn't support enum with string value, in this blog post, we'll look at alternatives and examples that you can use in code to make your life easier. The most popular string enum alternatives are: Use a public static readonly string. Custom Enumeration Class.
An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With