C# 从值枚举字符串名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/309333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 22:38:33  来源:igfitidea点击:

Enum String Name from Value

c#enums

提问by jdee

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 2the result should be Visible.

例如,给定2结果应该是Visible

采纳答案by Kent Boogaart

You can convert the intback to an enumeration member with a simple cast, and then call ToString():

您可以int使用简单的强制转换将返回值转换为枚举成员,然后调用ToString()

int value = GetValueFromDb();
var enumDisplayStatus = (EnumDisplayStatus)value;
string stringValue = enumDisplayStatus.ToString();

回答by Hath

you can just cast it

你可以投它

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

ah.. kent beat me to it :)

啊..肯特打败了我:)

回答by lacop

Just cast the int to the enumeration type:

只需将 int 转换为枚举类型:

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

回答by lacop

DB to C#

数据库到 C#

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

C# to DB

C# 到数据库

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

回答by Mandoleen

Try this:

尝试这个:

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

回答by algreat

If you need to get a string "Visible"without getting EnumDisplayStatusinstance you can do this:

如果您需要在"Visible"不获取EnumDisplayStatus实例的情况下获取字符串,则可以执行以下操作:

int dbValue = GetDBValue();
string stringValue = Enum.GetName(typeof(EnumDisplayStatus), dbValue);

回答by James Cooke

Use this:

用这个:

string bob = nameof(EnumDisplayStatus.Visible);

回答by Al3x_M

Just need:

只需要:

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

回答by Naveen Kumar V

SOLUTION:

解决方案:

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

Also, using GetNameis better than Explicit casting of Enum.

此外,使用GetName显式转换 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

回答by Muhammad Aqib

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