枚举缺少最后一项
我正在寻找一些枚举选项,只是发现最后一个选项后面缺少一个逗号。以 DayOfWeek
枚举为例(按 F12
转到定义):
public enum DayOfWeek
{
Sunday = 0,
Monday = 1,
Tuesday = 2,
Wednesday = 3,
Thursday = 4,
Friday = 5,
Saturday = 6, // note this comma
}
这种行为背后有什么原因吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最后一个
enum
成员后面的逗号是可选的。这样做大概是为了帮助自动代码生成,它可以直接输出内容而不考虑语法。无论逗号是否存在都不会改变任何东西。
看一下语法:
As您会看到,enum-body 包含单个尾随逗号的选项。
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:
As you see, the enum-body includes the option of a single trailing comma.
我很确定一周中没有一天被遗漏.. :)
您可以在枚举定义的末尾有一个可选的逗号(没有逗号也可以)。我想这只是一个方便的事情:如果世界发生变化,你必须在一周中添加一天:添加一个新行(并且可以选择再次以逗号结束,以便下一个定义新日期的人)。
I'm pretty sure no day of week is missing.. :)
You can have an optional comma at the end of an enum definition (works without as well). I guess that's just a convenience thing: If the world changes and you have to add a day to the week: Add a new line (and optionally end it with a comma again, for the next guy defining new days).
编译器只是忽略最后一个逗号。它不需要在那里,但如果你把它放在那儿也不是错误
The compiler just ignores the last comma. It doesn't need to be there, but it's not an error either if you put it