是否允许有一个以数字开头的枚举成员名称? [英] Is is allowed to have an Enum member name that start with a number?
问题描述
使用System.ComponentModel,这将是最直观的;
public enum Gauge
{
[描述(24 Gauge)]
24Ga = 239,
[描述(20 Gauge)]
20Ga = 359,
[描述(18 Gauge)]
18Ga = 478,
[描述(16 Gauge)]
16Ga = 598,
[描述(14 Gauge)]
14Ga = 747
}
但是这被Visual Studio拒绝。
相反,这是可以的:
使用System.ComponentModel;
public enum Gauge
{
[描述(24 Gauge)]
G24 = 239,
[描述(20 Gauge)]
G20 = 359,
[描述(18 Gauge)]
G18 = 478,
[描述(16 Gauge)]
G16 = 598,
[描述(14 Gauge)]
G14 = 747
}
这使得看起来很明显的是,您不能使用数字字符启动枚举成员名称,即使通常允许使用变量名称。
我的问题是:这个限制在哪里? Enum Class文档似乎没有提到它。
是否允许有一个以枚数开头的枚举成员名一个数字?
不,语言语法不允许。
请参见 2.4.2标识符
identifier-start-character:
letter-character
_(下划线字符U + 005F)
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.
Is is allowed to have an Enum member name that start with a number?
No, the language syntax does not allow it.
identifier-start-character:
letter-character
_ (the underscore character U+005F)
这篇关于是否允许有一个以数字开头的枚举成员名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!