是否可以将带有空格或特殊字符的字符串添加到枚举中?
是否可以将带有空格或特殊字符的字符串添加到枚举中?
例如,我有一个字符串 Insurance KR Users (Name)
,我尝试将此字符串包含到枚举中:
public enum MemberGroup
{
Insurance KR Users (Name)
}
但它会引发错误。
如何将这些类型的字符串包含到枚举中?
Is it possible to add a string with spaces or special characters to an enum?
For example, I have a string Insurance KR Users (Name)
, I have tried to include this string into an enum:
public enum MemberGroup
{
Insurance KR Users (Name)
}
but it throws an error.
How can I include these types of strings into enum?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
枚举成员本身必须是有效标识符,因此不能包含空格或特殊字符。
但是您可以使用
DescriptionAttribute
为每个枚举值提供更完整的描述:要检索描述,请使用如下内容:
The enum members itself must be a valid identifier, so it can't contain spaces or special characters.
But you could use the
DescriptionAttribute
to provide a more complete description of each enum value:To retrieve the description, use something like this:
枚举本身不能包含空格,正如 Peter 所说,它只能包含一组特定的字符。
但是,您可以使用枚举的 Description 属性来存储一些额外的信息,但这必须使用反射来检索。互联网上有很多如何做到这一点的示例,但仅作为示例, 这里就是其中之一。
The enum itself can't contain spaces, and as Peter says, it can only contain a certain set of characters.
You could use the enum's Description attribute however to store some extra information, but this would have to be retrieved using reflection. There are quite a few examples over the internet how to do it, but just as an example, here is one.