枚举可以包含字符串吗?
如何声明一个包含值字符串的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不,他们不能。它们仅限于基础枚举类型的数值。
但是,您可以通过辅助方法获得类似的行为
No they cannot. They are limited to numeric values of the underlying enum type.
You can however get similar behavior via a helper method
正如其他人所说,不,你不能。
您可以像这样创建静态类:
或者
使用 readonly,您实际上可以在静态构造函数中分配值。当使用const时,它必须是一个固定的字符串。
或者将 DescriptionAttribute 分配给枚举值,例如 此处。
As others have said, no you cannot.
You can do static classes like so:
Or
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.
否,但您可以获取字符串形式的枚举值:
Enum.ToString方法
No, but you can get the enum's value as a string:
Enum.ToString Method
默认情况下,枚举具有整数作为基础类型,这也被声明为 此处在 msdn 上。
An enum has integer as underlying type by default, which is also stated here on msdn.
也许
Enum.GetName()/Enum.GetNames()
对您有帮助。Maybe
Enum.GetName()/Enum.GetNames()
can be helpful for you.您可以创建枚举字典。
You can create a dictionary of enums.