C# using numbers in an enum

Viewed 89112

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

8 Answers

Short and crisp 4 line code.

We simply use enums as named integer for items in code,

so any simplest way is good to go.

public enum myEnum
{
    _1 = 1,
    _2,
    _3,
};

Also for decimal values,

public enum myEnum
{
    _1_5 = 1,
    _2_5,
    _3_5,
};

So while using this in code,

int i = cmb1.SelectedIndex(0); // not readable
int i = cmb1.SelectedIndex( (int) myEnum._1_5); // readable
Related