This is a valid enum
public enum myEnum
{
a= 1,
b= 2,
c= 3,
d= 4,
e= 5,
f= 6,
g= 7,
h= 0xff
};
But this is not
public enum myEnum
{
1a = 1,
2a = 2,
3a = 3,
};
Is there a way I can use an number in a enum? I already have code that would populate dropdowns from enums so it would be quite handy
No identifier at all in C# may begin with a number (for lexical/parsing reasons). Consider adding a [Description] attribute to your enum values:
Then you can get the description from an enum value like this:
Based on XSA’s comment below, I wanted to expand on how one could make this more readable. Most simply, you could just create a static (extension) method:
It’s up to you whether you want to make it an extension method, and in the implementation above, I’ve made it fallback to the enum’s normal name if no
[DescriptionAttribute]has been provided.Now you can get the description for an enum value via: