C# 如何在 Enum 上设置空间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1101872/
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
How to set space on Enum
提问by
I want to set the space on my enum. Here is my code sample:
我想在我的枚举上设置空间。这是我的代码示例:
public enum category
{
goodBoy=1,
BadBoy
}
I want to set
我想设置
public enum category
{
Good Boy=1,
Bad Boy
}
When I retrieve I want to see Good Boyresult from the enum
当我检索时,我想从枚举中看到Good Boy结果
回答by CMS
That's not possible, an enumerator cannot contain white space in its name.
这是不可能的,枚举器的名称中不能包含空格。
回答by Brendan Kowitz
Think this might already be covered, some suggestions:
认为这可能已经涵盖,一些建议:
- How do I have an enum bound combobox with custom string formatting for enum values?
- C# Getting Enum values
Just can't beat stackoverflow ;) just sooo much on here nowdays.
只是无法击败 stackoverflow ;) 现在这里太多了。
回答by Soviut
You are misunderstanding what an enum is used for. An enum is for programming purposes, essentially giving a name to a number. This is for the programmer's benefit while reading the source code.
您误解了枚举的用途。枚举用于编程目的,本质上是为数字命名。这是为了程序员在阅读源代码时的利益。
status = StatusLevel.CRITICAL; // this is a lot easier to read...
status = 5; // ...than this
Enums are notmeant for display purposes and should not be shown to the end user. Like any other variable, enums cannot use spaces in the names.
枚举不用于显示目的,不应向最终用户显示。与任何其他变量一样,枚举不能在名称中使用空格。
To associate internal values with "pretty" labels you can display to a user, can use a dictionary or hash.
要将内部值与可以显示给用户的“漂亮”标签相关联,可以使用字典或哈希。
myDict["Bad Boy"] = "joe blow";
回答by Cyril Gupta
You cannot have enum with spaces in .Net. This was possible with earlier versions of VB and C++ of course, but not any longer. I remember that in VB6 I used to enclose them in square brackets, but not in C#.
在 .Net 中不能有带空格的枚举。这当然在早期版本的 VB 和 C++ 中是可能的,但不再是了。我记得在 VB6 中我曾经将它们括在方括号中,但在 C# 中没有。
回答by donmezburak
Why don't you use ToString() ?
为什么不使用 ToString() ?
I mean that when use ToString(),it gives the enum value. Just you have to add some identifier to catch space.For example:
我的意思是当使用 ToString() 时,它给出了枚举值。只是你必须添加一些标识符来捕获空间。例如:
public enum category
{
good_Boy=1,
Bad_Boy
}
When you get an enum in codes like category a = ..., you can use ToString() method. It gives you value as a string. After that, you can simply change _ to empty string.
当您在类别 a = ... 等代码中获得枚举时,您可以使用 ToString() 方法。它为您提供字符串形式的值。之后,您可以简单地将 _ 更改为空字符串。
回答by Luke T O'Brien
You can decorate your Enum values with DataAnnotations
, so the following is true:
您可以使用 装饰您的 Enum 值DataAnnotations
,因此以下情况为真:
using System.ComponentModel.DataAnnotations;
public enum Boys
{
[Display(Name="Good Boy")]
GoodBoy,
[Display(Name="Bad Boy")]
BadBoy
}
I'm not sure what UI Framework you're using for your controls, but ASP.NET MVC can read DataAnnotations
when you type HTML.LabelFor
in your Razor views.
我不确定您用于控件的 UI 框架是什么,但是DataAnnotations
当您输入HTML.LabelFor
Razor 视图时,ASP.NET MVC 可以读取。
Here' a Extension method
这是一个扩展方法
If you are not using Razor views or if you want to get the names in code:
如果您不使用 Razor 视图,或者您想在代码中获取名称:
public class EnumExtention
{
public Dictionary<int, string> ToDictionary(Enum myEnum)
{
var myEnumType = myEnum.GetType();
var names = myEnumType.GetFields()
.Where(m => m.GetCustomAttribute<DisplayAttribute>() != null)
.Select(e => e.GetCustomAttribute<DisplayAttribute>().Name);
var values = Enum.GetValues(myEnumType).Cast<int>();
return names.Zip(values, (n, v) => new KeyValuePair<int, string>(v, n))
.ToDictionary(kv => kv.Key, kv => kv.Value);
}
}
Then use it:
然后使用它:
Boys.GoodBoy.ToDictionary()
回答by Mauro Quercioli
Since the original question was asking for adding a space within the enum value/name, I would say that the underscore character should be replaced with a space, not an empty string. However, the best solution is the one using annotations.
由于最初的问题是要求在枚举值/名称中添加一个空格,我会说下划线字符应该替换为空格,而不是空字符串。但是,最好的解决方案是使用注释。
回答by Devendra Gohel
An enumerator cannot contain white space in its name.
枚举器的名称中不能包含空格。
As we know that enum is keyword used to declare enumeration.
我们知道 enum 是用于声明枚举的关键字。
you can check throw this link https://msdn.microsoft.com/en-us/library/sbbt4032.aspx
您可以检查抛出此链接 https://msdn.microsoft.com/en-us/library/sbbt4032.aspx
回答by Veverke
Developing on user14570's (nice) workaround referred above, here's a complete example:
在上面提到的 user14570 的(不错的)解决方法上进行开发,这是一个完整的示例:
public enum MyEnum
{
My_Word,
Another_One_With_More_Words,
One_More,
We_Are_Done_At_Last
}
internal class Program
{
private static void Main(string[] args)
{
IEnumerable<MyEnum> values = Enum.GetValues(typeof(MyEnum)).Cast<MyEnum>();
List<string> valuesWithSpaces = new List<string>(values.Select(v => v.ToString().Replace("_", " ")));
foreach (MyEnum enumElement in values)
Console.WriteLine($"Name: {enumElement}, Value: {(int)enumElement}");
Console.WriteLine();
foreach (string stringRepresentation in valuesWithSpaces)
Console.WriteLine(stringRepresentation);
}
}
Output:
输出:
回答by Xtian11
using System.ComponentModel;
then...
然后...
public enum category
{
[Description("Good Boy")]
goodboy,
[Description("Bad Boy")]
badboy
}
Solved!!
解决了!!