Enum String Name from Value
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
.
2
Visible
nameof(EnumDisplayStatus.Visible)
8 Answers
8
You can convert the int
back to an enumeration member with a simple cast, and then call ToString()
:
int
ToString()
int value = GetValueFromDb();
EnumDisplayStatus enumDisplayStatus = (EnumDisplayStatus)value;
string stringValue = enumDisplayStatus.ToString();
Update: only certain overloads using IFormatProvider are deprecated. ToString() is fine. See groups.google.com/group/DotNetDevelopment/browse_thread/thread/…
– AndyM
Apr 15 '09 at 9:37
What is the behavior in case of
enum Foo A = 1, B= 1
?– dbkk
Jul 5 '11 at 6:26
enum Foo A = 1, B= 1
@dbkk the documentation states that with regards to enums "your code should not make any assumptions about which string will be returned." because of the precise situation you quote. see msdn.microsoft.com/en-us/library/16c1xs4z.aspx
– Paul D'Ambra
Jul 25 '11 at 12:41
An uptodated solution: msdn.microsoft.com/en-us/library/…
– Jack
Jul 18 '14 at 1:13
shorter: var stringValue = ((EnumDisplayStatus)value).ToString()
– redwards510
Jan 12 '17 at 18:17
If you need to get a string "Visible"
without getting EnumDisplayStatus
instance you can do this:
"Visible"
EnumDisplayStatus
int dbValue = GetDBValue();
string stringValue = Enum.GetName(typeof(EnumDisplayStatus), dbValue);
This answer is already posted (strangely you have more votes)..
– nawfal
Jun 8 '13 at 23:18
If you about Mandoleen's answer there is an inaccuracy: Enum.GetName returns a string, not an Enum
– algreat
Jun 9 '13 at 14:10
oh yes, you're right, so +1 for u too :)
– nawfal
Jun 9 '13 at 14:30
Mandoleen's answer has now been corrected
– niico
Mar 20 '17 at 13:14
Try this:
string m = Enum.GetName(typeof(MyEnumClass), value);
This should be the answer.
– niico
Mar 20 '17 at 13:13
@nilco this answer is good, but I prefer Kents' answer, mainly because I have a custom attribute on my enums "Description" I then have an enum extension to get the description - this is for displaying on screen for the user.
– Harag
Apr 12 at 15:10
you can just cast it
int dbValue = 2;
EnumDisplayStatus enumValue = (EnumDisplayStatus)dbValue;
string stringName = enumValue.ToString(); //Visible
ah.. kent beat me to it :)
Use this:
string bob = nameof(EnumDisplayStatus.Visible);
This is so simple, should be marked solution!
– AZ Chad
Jun 22 at 18:31
C# 6+ required though.
– Adam K Dean
Jul 17 at 18:32
@AZChad it is a great thing to know, sure; but it doesn't really apply in the OP's scenario, since the values are coming from a database (so: runtime, not compile-time, values)
– Marc Gravell♦
Nov 29 at 12:52
DB to C#
EnumDisplayStatus status = (EnumDisplayStatus)int.Parse(GetValueFromDb());
C# to DB
string dbStatus = ((int)status).ToString();
Just cast the int to the enumeration type:
EnumDisplayStatus status = (EnumDisplayStatus) statusFromDatabase;
string statusString = status.ToString();
Just need:
string stringName = EnumDisplayStatus.Visible.ToString("f");
// stringName == "Visible"
in most cases, this is pretty much identical to the top answer from 10 years ago; the addition of the
"f"
specifier is nuanced, and may or may not be what the caller wants (it depends on: what they want): docs.microsoft.com/en-us/dotnet/standard/base-types/…– Marc Gravell♦
Nov 29 at 12:50
"f"
I didn't pay attention to the date ahah. I think it is good to update a bit the old solution like this one. I won't be the last one to open this page. And thanks for your precision! :)
– Al3x_M
Nov 29 at 16:39
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
I believe, now there is an easier way to do this with VS 2015
nameof(EnumDisplayStatus.Visible)
Hope it helps somebody– Gabriel Marius Popescu
May 23 '17 at 9:08