C#: any c# – .NET Enumeration allows comma in the last field – Stack Overflow
Posted by jpluimers on 2012/12/06
Thanks Nick Craver for answering this on StackOverflow.
Array initializers can be specified in field declarations (§17.4), local variable declarations (§15.5.1), and
array creation expressions (§14.5.10.2).
The array initializer can end in a comma, which makes some things way easier (boy, I wish I had this in other programming languages).
From Nick’s answer:
It has no special meaning, just the way the compiler works, it’s mainly for this reason:
[FlagsAttribute]
public enum DependencyPropertyOptions : byte
{
Default = 1,
ReadOnly = 2,
Optional = 4,
DelegateProperty = 32,
Metadata = 8,
NonSerialized = 16,
//EnumPropertyIWantToCommentOutEasily = 32
}
[/language]By comment request: This info comes straight out of the ECMA C# Specification (Page 363/Section 19.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.”
–jeroen
via c# – .NET Enumeration allows comma in the last field – Stack Overflow.
Denis said
IMO Delphi should have this too.
François said
Work-around: put the comma at the beginning of the lines (except line #1). Not as dandy but still useful… (in uses clause too)
jpluimers said
That’s not a true workaround, it is just moving the problem from the last entry to the first entry (:
jachguate said
In Delphi/Pascal, I’m used to put the comma at the very begining of each line like this:
That way, I’m in trouble only if I want to comment the first enumeration value. I know it looks very ugly at the beginning, but it’s convenience made my eyes (and brain) to like the way it looks.
jpluimers said
In Delphi you’re either in trouble at the first element, or the last one (;