-2

I am trying to set up an enum to hold sheet metal gauges (thicknesses). I thought it would be most intuitive if I could write:

using System.ComponentModel;
public enum Gauge
{
    [Description("24 Gauge")]
    24Ga = 239,
    [Description("20 Gauge")]
    20Ga = 359,
    [Description("18 Gauge")]
    18Ga = 478,
    [Description("16 Gauge")]
    16Ga = 598,
    [Description("14 Gauge")]
    14Ga = 747
}

but this is rejected by Visual Studio.

Instead this is OK:

using System.ComponentModel;
public enum Gauge
{
    [Description("24 Gauge")]
    G24 = 239,
    [Description("20 Gauge")]
    G20 = 359,
    [Description("18 Gauge")]
    G18 = 478,
    [Description("16 Gauge")]
    G16 = 598,
    [Description("14 Gauge")]
    G14 = 747
}

This makes it seem pretty obvious that you are not allowed to start a Enum member name off with a numeric character, even though that is usually allowed for variable names.

My question is: Where is this restriction specified? The Enum Class documentation does not seem to mention it.

gunr2171
  • 16,104
  • 25
  • 61
  • 88
Mike
  • 1,274
  • 10
  • 24
  • "*[...] Decimal type. Isn't it a 64 bit integer with the decimal place shifted behind the scenes?*" Read up on it in the [MSDN documentation for *Decimal*](http://msdn.microsoft.com/en-us/library/vstudio/system.decimal.aspx), please... –  Aug 08 '14 at 20:28
  • @gunr2171 Shoot, I want to roll back these changes since I shifted the second part to a new question, but I can't seem to do that. Would a moderator please roll back the changes to this . . . – Mike Aug 08 '14 at 20:38
  • I used to use numbers in that position in other languages, but now I think this was a stupid question. – Mike Aug 08 '14 at 20:41
  • 1
    "Even though that is usually allowed for variable names." **No it isn't.** – clcto Aug 08 '14 at 20:53

2 Answers2

3

Is is allowed to have an Enum member name that start with a number?

No, the language syntax does not allow it.

See 2.4.2 Identifiers

identifier-start-character:
    letter-character
    _ (the underscore character U+005F) 
AlexD
  • 32,156
  • 3
  • 71
  • 65
3

No. Enum members have to be valid identifiers. From section 14.3 of the C# 5 specification:

The body of an enum type declaration defines zero or more enum members, which are the named constants of the enum type. No two enum members can have the same name.

enum-member-declarations:
enum-member-declaration
enum-member-declarations , enum-member-declaration
enum-member-declaration:
attributesopt identifier
attributesopt identifier = constant-expression

... and identifiers are described in section 2.4.2:

identifier:
available-identifier
@identifier-or-keyword
available-identifier:
An identifier-or-keyword that is not a keyword
identifier-or-keyword:
identifier-start-character identifier-part-charactersopt
identifier-start-character:
letter-character
_ (the underscore character U+005F)

Note how a digit is not an identifier-start-character.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194