Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# using numbers in an enum

Tags:

syntax

c#

enums

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

like image 322
DrLazer Avatar asked Oct 12 '10 16:10

DrLazer


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C and C++ same?

While C and C++ may sound similar, their features and usage differ. C is a procedural programming language that support objects and classes. On the other hand C++ is an enhanced version of C programming with object-oriented programming support.


2 Answers

No, there isn't. C# does not allow identifiers to start with a digit.

Application usability note: In your application you should not display code identifiers to the end-user anyway. Think of translating individual enumeration items into user-friendly displayable texts. Sooner or later you'll have to extend the enum with an item whose identifier won't be in a form displayable to the user.

UPDATE: Note that the way for attaching displayable texts to enumeration items is being discusses, for example, here.

like image 21
Ondrej Tucny Avatar answered Sep 24 '22 12:09

Ondrej Tucny


No identifier at all in C# may begin with a number (for lexical/parsing reasons). Consider adding a [Description] attribute to your enum values:

public enum myEnum {     [Description("1A")]     OneA = 1,     [Description("2A")]     TwoA = 2,     [Description("3A")]     ThreeA = 3, }; 

Then you can get the description from an enum value like this:

((DescriptionAttribute)Attribute.GetCustomAttribute(     typeof(myEnum).GetFields(BindingFlags.Public | BindingFlags.Static)         .Single(x => (myEnum)x.GetValue(null) == enumValue),         typeof(DescriptionAttribute))).Description 

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:

public static string GetDescription(this Enum value) {     return ((DescriptionAttribute)Attribute.GetCustomAttribute(         value.GetType().GetFields(BindingFlags.Public | BindingFlags.Static)             .Single(x => x.GetValue(null).Equals(value)),         typeof(DescriptionAttribute)))?.Description ?? value.ToString(); } 

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:

myEnum.OneA.GetDescription() 
like image 79
Kirk Woll Avatar answered Sep 22 '22 12:09

Kirk Woll