问题
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