c# - C# 枚举中的空格

标签 c# vb.net enums clr

有没有办法在 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/

相关文章:

c# - 使用 C# 在 SQL Server 中存储图像

c# - 如何从标志枚举中获取数值?

java - 为什么我无法将我的枚举声明为公共(public)枚举?

c# - 使用 XDocument 和 XPath 解析 xml

c# - Websocket 的 ReceiveAsync 方法不等待整个消息

c# - 是否有 WebDriver Navigated 事件?

spring - Spring ConversionFailedException 错误 HTTP 状态的自定义

mysql - 我该如何解决这个异常?

.net - 尝试传入 List(Of clsFooDetail) 类型的对象时,VB.Net 中的 ByRef 下划线带有 "Expression Expected"错误

vb.net - 在 Vb.Net 中自动生成 XML 注释