C# 枚举中的空格 [英] Spaces in C# Enums

查看:69
本文介绍了C# 枚举中的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 C# 枚举常量中放置空格?我已经读到你可以通过这样做在 VB 中做到这一点:

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

这对我来说意味着 CLR 可以处理带有空格的枚举.

That implies to me that the CLR can handle an enum with spaces.

有没有办法在 C# 中做到这一点?

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/

来自文章:

但是枚举在 C# 中不能有空格!"你说.嗯,我喜欢用System.ComponentModel.DescriptionAttribute添加更友好的描述枚举值.示例枚举可以改写成这样:

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() 版本显示正常的项目上添加描述.

Notice that I do not put descriptions on items where the ToString() version of that item displays just fine.

这篇关于C# 枚举中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆