Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 枚举可以包含字符串吗?_C#_String_Enums - Fatal编程技术网

C# 枚举可以包含字符串吗?

C# 枚举可以包含字符串吗?,c#,string,enums,C#,String,Enums,如何声明具有值字符串的enum private enum breakout { page = "String1", column = "String2", pagenames = "String3", row = "String4" } 不,他们不能。它们仅限于基础枚举类型的数值 但是,您可以通过助手方法获得类似的行为 public static string GetStringVersion(breakout value) { sw

如何声明具有值字符串的
enum

private enum breakout {            
    page = "String1",
    column = "String2",
    pagenames = "String3",
    row = "String4"
}

不,他们不能。它们仅限于基础枚举类型的数值

但是,您可以通过助手方法获得类似的行为

public static string GetStringVersion(breakout value) {
  switch (value) {
    case breakout.page: return "String1";
    case breakout.column: return "String2";
    case breakout.pagenames: return "String3";
    case breakout.row: return "String4";
    default: return "Bad enum value";
  }
}

默认情况下,枚举的基础类型为integer,msdn上也有说明。

正如其他人所说,不,您不能

您可以这样做静态类:

internal static class Breakout {
    public static readonly string page="String1";
    public static readonly string column="String2";
    public static readonly string pagenames="String3";
    public static readonly string row="String4";

    // Or you could initialize in static constructor
    static Breakout() {
        //row = string.Format("String{0}", 4);
    }
}

使用readonly,您实际上可以在静态构造函数中赋值。使用常量时,它必须是固定字符串


或者为枚举值指定一个值,例如。

您可以创建枚举字典

public enum OrderType
{
    ASC,
    DESC
}

public class MyClass
{
    private Dictionary<OrderType, string> MyDictionary= new Dictionary<OrderType, string>()
                                                     {
                                                         {OrderType.ASC, ""},
                                                         {OrderType.DESC, ""},
                                                     };
}
公共枚举医嘱类型
{
ASC,
描述
}
公共类MyClass
{
专用词典MyDictionary=新词典()
{
{OrderType.ASC,“},
{OrderType.DESC,“},
};
}

否,但您可以以字符串形式获取枚举值:

private enum Breakout {     
  page,
  column,
  pagenames,
  row
}

Breakout b = Breakout.page;
String s = b.ToString(); // "page"

也许
Enum.GetName()/Enum.GetNames()
会对您有所帮助。

+1使用helper方法很好
ToString(“F”)
对于格式化为枚举值的名称非常有用,如果他们不需要自定义字符串。开关的注意事项:如果省略默认大小写,则如果使用新值更新枚举,编译器将警告您开关也需要更新。这很方便。@Chris我不相信是这样的。如果没有
default
标签,C#编译器只会在所有代码执行时发出缺少返回的警告/错误paths@Chris我相信某些工具,如R#或FxCop可能会提供这种行为。我不记得了,但我会使用“只读”或“常量”。+1,尽管在这种情况下,
const
static
更好。@vcsjones&@Davy-很好。我本想包含readonly,但没有包含。description属性为+1。这些方法通过反射工作。在这一点上,您还可以使用程序集元数据,因为它的访问速度很慢。使用枚举的原因之一是它们的性能。
private enum Breakout {     
  page,
  column,
  pagenames,
  row
}

Breakout b = Breakout.page;
String s = b.ToString(); // "page"