在C#中枚举空间 [英] Spaces in C# Enums
问题描述
有没有什么办法可以把空间在C#枚举常量?我读过,你可以在VB中做到这一点做这个:
公共枚举EnumWithSpaces
ConstantWithoutSpaces
[常量用空格]
结束枚举
...然后访问它是这样的:
公用Sub UsingEnumWithSpaces() 昏暗的富作为EnumWithSpaces = EnumWithSpaces。[常量用空格]结束小组
这意味着对我来说,CLR可以处理带有空格的枚举。
有没有办法在C#这样做吗?
本博客文章可以帮助你:
<一个href=\"http://blog.spontaneouspublicity.com/2008/01/17/associating-strings-with-enums-in-c/\">http://blog.spontaneouspublicity.com/2008/01/17/associating-strings-with-enums-in-c/
块引用>从文章:
但枚举不能有空格在C#!
你说。嗯,我喜欢用
System.ComponentModel.DescriptionAttribute
一个更友好的描述增加
枚举值。这个例子可以枚举
被改写这样的:
块引用>公开枚举美国
{
加利福尼亚州,
[说明(新墨西哥)]
新墨西哥,
[说明(纽约)]
纽约,
[说明(南卡罗来纳州)]
南卡罗来纳,
田纳西州,
华盛顿
}请注意,我不把描述上的项目,其中
的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屋!