Display Text for Enum [duplicate]

微笑、不失礼 提交于 2020-07-30 02:40:10

问题


HI
I have the following enum

public enum Priority : byte 
    {
        A=1,
        B+ = 2,
        B=4,
        C=8,
        D=16,
        E=32
    }

I want to add B+ in the enum but it is giving me error


回答1:


You can add user friendly description for enum like below :

enum MyEnum 
{ 
    [Description("This is black")] 
    Black, 
    [Description("This is white")] 
    White 
} 

Ref. Link : How to have userfriendly names for enumerations?




回答2:


How about using a valid identifier like B_Plus?




回答3:


Yes. It's giving you an error because your code is wrong. You can't make "B+" an enum value because there's a plus sign. Same reason you can't declare int B+. Use a different name.




回答4:


You won't be able to use + as a name identifier because it's a math operator or string concatenator... it can't be used with enums. Use an alternative syntax, or use an alternative approach. You could consider a state design pattern:

http://www.dofactory.com/patterns/PatternState.aspx#_self2



来源:https://stackoverflow.com/questions/8697890/display-text-for-enum

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!