C#枚举中的空格 [英] Spaces in C# Enums
问题描述
公共枚举EnumWithSpaces
ConstantWithoutSpaces
[常数与空格]
结束枚举
...然后访问它像这样:
Public Sub UsingEnumWithSpaces()
Dim foo As EnumWithSpaces = EnumWithSpaces。[Constant with Spaces]
End Sub
这意味着CLR可以处理枚举与空格。
有没有办法在C#中执行此操作?
这个博文可能会帮助你:
http://blog.spontaneouspublicity.com/2008/01/17/associating-strings-with-enums-in-c/
从文章:
但是枚举不能在C#中有空格!
你说,我喜欢使用
System.ComponentModel.DescriptionAttribute
为
添加一个更友好的描述枚举值枚举可以
重写如下:
public enum国家
{
加州,
[描述(新墨西哥州)]
NewMexico,
[描述(纽约)]
纽约,
[说明(南卡罗来纳州)]
南卡罗来纳州,
Tenne看到
华盛顿
}
请注意,我没有对项目进行说明其中 ToString()
版本的项目显示正常。
Is there any way to put spaces in a C# enum constant? I've read that you can do it in VB by doing this:
Public Enum EnumWithSpaces
ConstantWithoutSpaces
[Constant With Spaces]
End Enum
...and then access it like this:
Public Sub UsingEnumWithSpaces()
Dim foo As EnumWithSpaces = EnumWithSpaces.[Constant With Spaces]
End Sub
That implies to me that the CLR can handle an enum with spaces.
Is there any way to do this in C#?
This blog post might help you:
http://blog.spontaneouspublicity.com/2008/01/17/associating-strings-with-enums-in-c/
From the article:
But enums can't have spaces in C#!" you say. Well, I like to use the System.ComponentModel.DescriptionAttribute to add a more friendly description to the enum values. The example enum can be rewritten like this:
public enum States
{
California,
[Description("New Mexico")]
NewMexico,
[Description("New York")]
NewYork,
[Description("South Carolina")]
SouthCarolina,
Tennessee,
Washington
}
Notice that I do not put descriptions on items where the ToString()
version of that item displays just fine.
这篇关于C#枚举中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!