如何在 C# 枚举中使用特殊字符?
例如:
public enum Unit{
KW,
kV,
V,
Hz,
%V
}
在本例中 % 是一个特殊字符。那么,如何将这个字符放入枚举中呢?
For example:
public enum Unit{
KW,
kV,
V,
Hz,
%V
}
In this case % is a special character. So, how can I put this char in a enum?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
即使您可以做到这一点(看起来您做不到),它也可能不是一个好主意,因为您会将枚举的显示方式与程序代码混合在一起以操作它。更好的选择是定义一个属性(或使用现有的 DisplayNameAttribute )并使用名称注释您的枚举作为附加元数据:
Even if you could do that (and it looks you can't), it probably wouldn't be a good idea, because you'd be mixing how the enum should be displayed with the program code to manipulate it. A better option would be to define an attribute (or use existing
DisplayNameAttribute
) and annotate your enum with names as additional meta-data:枚举成员不应用于用户界面显示目的。它们应该映射到一个字符串才能显示。您可以创建一个字符串数组(或字典),将每个枚举成员映射到一个字符串以供用户交互。
也就是说,要直接回答您的问题,您可以使用正如 Henk 指出的,这对\uxxxxV
,其中xxxx
是代表%
。这远非推荐的做法。%
不起作用,因为它不在 Unicode 类 Lu、Ll、Lt、Lm、Lo、Nl、Mn、Mc 中、Nd、Pc、Cf(字母、数字、连接和格式字符)。只有这些字符才可以作为标识符。Enum members shouldn't be used for user interface display purposes. They should be mapped to a string in order to get displayed. You can create a string array (or a dictionary) that maps each enum member to a string for user interaction.
That said, to answer your question directly, you can useAs Henk points out, this won't work for\uxxxxV
werexxxx
is the hexadecimal number representing the Unicode code point for%
. This is far from recommended.%
as it's not in Unicode classes Lu, Ll, Lt, Lm, Lo, Nl, Mn, Mc, Nd, Pc, Cf (letters, digits, connecting, and formatting characters). Only these characters are acceptable for identifiers.只是为了注册另一种方法来做到这一点,以一种简单的方式,您可以使用常量定义“您自己的”枚举器。在您的示例中
要访问,只需:
UnitEnum.Volt
Just to register another way to do that, in a simple way, you can define "your own" enumerator with constants. In your example
To access, it just:
UnitEnum.Volt
这个答案与@Coppermill 的答案相关,我觉得在使用 Enums 时使用 DescriptionAttribute 在语义上更正确
然后我像这样阅读它
。
然后用法是 ReadDescription(ReportStatus.Running) 我还有一个方法,可以将 Enum 转换为 KeyValuePair Enumerable,以便将 Enum 绑定到 DropDown。
This answer is related to the one from @Coppermill I feel using the DescriptionAttribute is more semantically correct when working with Enums
Then I read from it like such
.
Then usage would be
ReadDescription(ReportStatus.Running)
I also have a method that will convert an Enum into a KeyValuePair Enumerable for binding an Enum to a DropDown.抱歉,我刚刚意识到我没有回答这个问题。我不会删除我的答案,因为有人可能会发现这些代码片段很有帮助。
我完全同意Tomas Petricek的观点,所以我不会重复他的回答。
这是我对问题的解决方案。我使用这段代码大约五年了。我决定创建一个自定义属性,以便将 DisplayName 属性用于标题等。
Sorry, but I just realized that I didn't answer the question. I will not delete my answer because someone may find these code snippets helpful.
I agree completely with Tomas Petricek, so I will not repeat his answer.
Here is my solution to the problem. I been using this code for about five years. I decided to create a custom attribute in order to use the DisplayName attribute for captions and such.
枚举成员不应用于用户界面显示目的。但是您可以使用简单技巧与DisplayName("Your Property Display Name")来解决这个问题,如下所示。
Enum members shouldn't be used for user interface display purposes. But you can use simple tricks with DisplayName("Your Property Display Name") for this matter as bellow.
有些人可能会说枚举仅用于代码,我必须不同意,我使用代码和显示功能。
在您的特定情况下,我会使用完整的单词
,因此我可以在下拉列表中使用它们,例如(当我需要创建一个新项目时,我需要做的就是将该项目附加到枚举中......
我倾向于使用空格分隔符,但我通常使用下划线来制作空格,就像
DropDownList 项一样,
当我想从 DropDown 设置类型时,我当然会使用解析
,如果您使用分隔符
一些规则考虑编写更好的代码
作为提醒
我可以帮助某人。
Some can state that Enumerations are for Code only, I must disagree and I use to Code and Display functionality.
In your particular case I would use the full word
So I can use them in a Dropdown for example as (when I need to create a new item, all I need to do is append that item into the Enumeration...
I tend to use Space Separator, but I normally use underscore to make spaces, like
and the DropDownList item would be
when I want to set the Type from the DropDown, I use the Parse
off course, if you use Separator
Some rules to have in consideration to write better code
As a reminder
I hope I can help someone.