C# 枚举中的空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1117542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:36:23  来源:igfitidea点击:

Spaces in C# Enums

c#vb.netenumsclr

提问by cdmckay

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:

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

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.

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

Is there any way to do this in C#?

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

采纳答案by Joel Marcey

This blog post might help you:

这篇博文可能对您有所帮助:

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/

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:

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

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.

请注意,我不会在该项目的ToString()版本显示正常的项目上添加描述。

回答by Pavel Minaev

CLR can handle absolutely any character in identifiers. However, C# restricts the identifier characters to those legal under the CLS, which space isn't. Same goes for VB.NET, by the way - spaces inside square brackets used to work in VB6, but they don't in VB.NET.

CLR 可以处理标识符中的任何字符。但是,C# 将标识符字符限制为那些在 CLS 下合法的字符,而空格不是。顺便说一下,VB.NET 也是如此——方括号内的空格曾经在 VB6 中工作,但在 VB.NET 中却没有。

回答by Micah Hahn

If you're working with Visual C# 3.0 or above I've found it convenient to just extend the enum class and use a regex to inset spaces where neccessary:

如果您使用的是 Visual C# 3.0 或更高版本,我发现扩展枚举类并使用正则表达式在必要的地方插入空格很方便:

public static class EnumExtension
{
    public static String ToDisplayString(this Enum e)
    {
        Regex regex = new Regex(@"([^\^])([A-Z][a-z$])");

        return regex.Replace(e.ToString(), new MatchEvaluator(m =>
        {
            return String.Format("{0} {1}", m.Groups[1].Value, m.Groups[2].Value);
        }));
    }
}

Notice this allows you to work with any enum as is without adding descriptions to every value.

请注意,这允许您按原样使用任何枚举,而无需为每个值添加描述。

String enumWithSpaces = MessageBoxButtons.OKCancel.ToDisplayString();