有没有办法在 C# 枚举常量中放置空格?我读过你可以通过这样做在 VB 中做到这一点:
Public Enum EnumWithSpaces
ConstantWithoutSpaces
[Constant With Spaces]
End Enum
...然后像这样访问它:
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/
来自文章:
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
}
请注意,我没有在项目的 ToString()
版本显示得很好的项目上放置描述。
关于c# - C# 枚举中的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1117542/