C# using numbers in an enum

前端 未结 8 1385
迷失自我
迷失自我 2020-11-30 03:02

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



        
8条回答
  •  一向
    一向 (楼主)
    2020-11-30 03:29

    An identifier in C# (and most languages) cannot start with a digit.

    If you can modify the code that populates a dropdown with the enumeration names, you could maybe have a hack that strips off a leading underscore when populating the dropdown and define your enum like so:

    public enum myEnum
    {
      _1a = 1,
      _2a = 2,
      _3a = 3
    };
    

    Or if you don't like the underscores you could come up with your own 'prefix-to-be-stripped' scheme (maybe pass the prefix to the constructor or method that will populate the dropdown from the enum).

提交回复
热议问题