缺少枚举上的最后一项 [英] Missing last item on enums

查看:162
本文介绍了缺少枚举上的最后一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一些枚举选项,并在最后一个选项后发现一个缺少的逗号。例如,以 DayOfWeek 枚举(按 F12 去定义):

  public enum DayOfWeek 
{
星期日= 0,
星期一= 1,
星期二= 2,
星期三= 3,
星期四= 4,
星期五= 5,
星期六= 6,//注意这个逗号
}

这个行为是否有任何理由?

解决方案

最后一个枚举之后的逗号成员是可选的。这可能是为了帮助自动代码生成,可以在不考虑语法的情况下吐出东西。



逗号是否存在,不会改变任何内容。 / p>

查看语法:


枚举声明:

&#x2003;&#x2003; 属性 opt &#x2003; -modifiers <子> 选择 &#x2003; 枚举&#x2003; 标识符&#x2003; 枚举基 <子> 选择 &#x2003; 枚举体&#x2003; ; opt



/ em>

&#x2003;&#x2003; 整数型



枚举体:

&#x2003;&#x2003; {枚举成员声明
opt }

&#x2003; #x2003; { 枚举成员声明 }



枚举成员声明:

&#x2003;&#x2003; 枚举成员声明

&#x2003;&#x2003; 枚举成员声明&#x2003; &#x2003; 枚举 - 成员声明



枚举成员声明:

&#x2003;&#x2003 ; 属性 opt &#x2003; 标识符

&#x2003;&#x2003 ; 属性 <子> 选择 &#x2003; 标识符&#x2003; = &#x2003; 常量表达式


如你所见,枚举-body 包含单个后缀逗号的选项。


I was looking for some enums options, and just spotted a missing comma after last option. Take, for instance, that DayOfWeek enum (press F12 to go to definition):

public enum DayOfWeek
{
    Sunday    = 0,
    Monday    = 1,
    Tuesday   = 2,
    Wednesday = 3,
    Thursday  = 4,
    Friday    = 5,
    Saturday  = 6, // note this comma
}

Are there any reason behind this behavior?

解决方案

The trailing comma after the last enum member is optional. This was presumably done to aid automatic code generation which can just spit out stuff without thinking about the grammar.

Whether the comma is present or not doesn't change anything.

Take a look at the grammar:

enum-declaration:
  attributesoptenum-modifiersoptenumidentifierenum-baseoptenum-body;opt

enum-base:
  : integral-type

enum-body:
  { enum-member-declarationsopt }
  { enum-member-declarations , }

enum-member-declarations:
  enum-member-declaration
  enum-member-declarations,enum-member-declaration

enum-member-declaration:
  attributesoptidentifier
  attributesoptidentifier=constant-expression

As you see, the enum-body includes the option of a single trailing comma.

这篇关于缺少枚举上的最后一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆