枚举可以包含字符串吗?

发布于 2024-11-14 21:27:09 字数 195 浏览 1 评论 0原文

如何声明一个包含值字符串的enum

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

How can I declare an enum that has strings for values?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

冬天旳寂寞 2024-11-21 21:27:09

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

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

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";
  }
}

No they cannot. They are limited to numeric values of the underlying enum type.

You can however get similar behavior via a helper method

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";
  }
}
花开半夏魅人心 2024-11-21 21:27:09

正如其他人所说,不,你不能。

您可以像这样创建静态类:

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

或者

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

使用 readonly,您实际上可以在静态构造函数中分配值。当使用const时,它必须是一个固定的字符串。

或者将 DescriptionAttribute 分配给枚举值,例如 此处

As others have said, no you cannot.

You can do static classes like so:

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

Or

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

Using readonly, you can actually assign the value in a static constructor. When using const, it must be a fixed string.

Or assign a DescriptionAttribute to enum values, like here.

枕头说它不想醒 2024-11-21 21:27:09

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

Enum.ToString方法

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

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

No, but you can get the enum's value as a string:

Enum.ToString Method

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

Breakout b = Breakout.page;
String s = b.ToString(); // "page"
笙痞 2024-11-21 21:27:09

默认情况下,枚举具有整数作为基础类型,这也被声明为 此处在 msdn 上。

An enum has integer as underlying type by default, which is also stated here on msdn.

遗忘曾经 2024-11-21 21:27:09

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

Maybe Enum.GetName()/Enum.GetNames() can be helpful for you.

流星番茄 2024-11-21 21:27:09

您可以创建枚举字典。

public enum OrderType
{
    ASC,
    DESC
}

public class MyClass
{
    private Dictionary<OrderType, string> MyDictionary= new Dictionary<OrderType, string>()
                                                     {
                                                         {OrderType.ASC, ""},
                                                         {OrderType.DESC, ""},
                                                     };
}

You can create a dictionary of enums.

public enum OrderType
{
    ASC,
    DESC
}

public class MyClass
{
    private Dictionary<OrderType, string> MyDictionary= new Dictionary<OrderType, string>()
                                                     {
                                                         {OrderType.ASC, ""},
                                                         {OrderType.DESC, ""},
                                                     };
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文