Enum String Name from Value

C#Enums

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

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

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

Solution 3 - C#

Try this:

string m = Enum.GetName(typeof(MyEnumClass), value);

Solution 4 - C#

Use this:

string bob = nameof(EnumDisplayStatus.Visible);

Solution 5 - C#

The fastest, compile time solution using nameof expression.

Returns the literal type casing of the enum or in other cases, a class, struct, or any kind of variable (arg, param, local, etc).

public enum MyEnum {
    CSV,
    Excel
}


string enumAsString = nameof(MyEnum.CSV)
// enumAsString = "CSV"

Note:

  • You wouldn't want to name an enum in full uppercase, but used to demonstrate the case-sensitivity of nameof.

Solution 6 - C#

you can just cast it

int dbValue = 2;
EnumDisplayStatus enumValue = (EnumDisplayStatus)dbValue;
string stringName = enumValue.ToString(); //Visible

ah.. kent beat me to it :)

Solution 7 - C#

SOLUTION:

int enumValue = 2; // The value for which you want to get string 
string enumName = Enum.GetName(typeof(EnumDisplayStatus), enumValue);

Also, using GetName is better than Explicit casting of Enum.

[Code for Performance Benchmark]

Stopwatch sw = new Stopwatch (); sw.Start (); sw.Stop (); sw.Reset ();
double sum = 0;
int n = 1000;
Console.WriteLine ("\nGetName method way:");
for (int i = 0; i < n; i++) {
   sw.Start ();
   string t = Enum.GetName (typeof (Roles), roleValue);
   sw.Stop ();
   sum += sw.Elapsed.TotalMilliseconds;
   sw.Reset ();
}
Console.WriteLine ($"Average of {n} runs using Getname method casting way: {sum / n}");
Console.WriteLine ("\nExplicit casting way:");
for (int i = 0; i < n; i++) {
   sw.Start ();
   string t = ((Roles)roleValue).ToString ();
   sw.Stop ();
   sum += sw.Elapsed.TotalMilliseconds;
   sw.Reset ();
}
Console.WriteLine ($"Average of {n} runs using Explicit casting way: {sum / n}");

[Sample result]

GetName method way:
Average of 1000 runs using Getname method casting way: 0.000186899999999998
Explicit casting way:
Average of 1000 runs using Explicit casting way: 0.000627900000000002

Solution 8 - C#

DB to C#

EnumDisplayStatus status = (EnumDisplayStatus)int.Parse(GetValueFromDb());

C# to DB

string dbStatus = ((int)status).ToString();

Solution 9 - C#

Just need:

string stringName = EnumDisplayStatus.Visible.ToString("f");
// stringName == "Visible"

Solution 10 - C#

i have used this code given below

 CustomerType = ((EnumCustomerType)(cus.CustomerType)).ToString()

Solution 11 - C#

For getting the String value [Name]:

EnumDisplayStatus enumDisplayStatus = (EnumDisplayStatus)GetDBValue();
string stringValue = $"{enumDisplayStatus:G}"; 

And for getting the enum value:

string stringValue = $"{enumDisplayStatus:D}";
SetDBValue(Convert.ToInt32(stringValue ));

Solution 12 - C#

Just cast the int to the enumeration type:

EnumDisplayStatus status = (EnumDisplayStatus) statusFromDatabase;
string statusString = status.ToString();

Solution 13 - C#

You can try this

string stringValue=( (MyEnum)(MyEnum.CSV)).ToString();

Solution 14 - C#

Given:

enum Colors {
	Red = 1,
	Green = 2,
	Blue = 3
};

In .NET 4.7 the following

Console.WriteLine( Enum.GetName( typeof(Colors), Colors.Green ) );
Console.WriteLine( Enum.GetName( typeof(Colors), 3 ) );

will display

Green
Blue

In .NET 6 the above still works, but also:

Console.WriteLine( Enum.GetName( Colors.Green ) );
Console.WriteLine( Enum.GetName( (Colors)3 ) );

will display:

Green
Blue

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjdeeView Question on Stackoverflow
Solution 1 - C#Kent BoogaartView Answer on Stackoverflow
Solution 2 - C#algreatView Answer on Stackoverflow
Solution 3 - C#MandoleenView Answer on Stackoverflow
Solution 4 - C#James CookeView Answer on Stackoverflow
Solution 5 - C#ReapView Answer on Stackoverflow
Solution 6 - C#HathView Answer on Stackoverflow
Solution 7 - C#Naveen Kumar VView Answer on Stackoverflow
Solution 8 - C#MisterTomView Answer on Stackoverflow
Solution 9 - C#Al3x_MView Answer on Stackoverflow
Solution 10 - C#BiddutView Answer on Stackoverflow
Solution 11 - C#Muhammad AqibView Answer on Stackoverflow
Solution 12 - C#lacopView Answer on Stackoverflow
Solution 13 - C#BiddutView Answer on Stackoverflow
Solution 14 - C#StackOverflowUserView Answer on Stackoverflow