c# - .NET 枚举允许在最后一个字段中使用逗号

标签 c# .net enums

为什么这个 .NET 枚举允许在最后一个字段中有一个逗号?
这有什么特殊意义吗?

[FlagsAttribute]
public enum DependencyPropertyOptions : byte
{
           Default = 1,
           ReadOnly = 2,
           Optional = 4,
           DelegateProperty = 32,
           Metadata = 8,
           NonSerialized = 16,
}

最佳答案

没有什么特别的意思,就是编译器的工作方式,主要是这个原因:

[FlagsAttribute]
public enum DependencyPropertyOptions : byte
{
           Default = 1,
           ReadOnly = 2,
           Optional = 4,
           DelegateProperty = 32,
           Metadata = 8,
           NonSerialized = 16,
           //EnumPropertyIWantToCommentOutEasily = 32
}

根据评论请求:此信息直接来自 C# Specification (Page 355/Section 17.7)

Like Standard C++, C# allows a trailing comma at the end of an array-initializer. This syntax provides flexibility in adding or deleting members from such a list, and simplifies machine generation of such lists.

关于c# - .NET 枚举允许在最后一个字段中使用逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2147333/

相关文章:

c# - ServiceStack DTO 程序集

c# - 将重复的 for 循环功能提取到自己的方法中

c# - 这里需要内存栅栏吗?

xna - 如何在 XNA/Monogame 中标记要删除的实例

c# - vstest.console.exe 忽略 app.config

.NET:获取从特定类派生的所有类

.net - 将自定义 .NET 代码与 DOTNETNUKE 一起使用

java - xsl 中枚举的值

Xcode 6 代码编辑器不稳定的行为

typescript - 如何将函数参数的类型与某个枚举值关联起来?