49

Why is this .NET enumeration allowed to have a comma in the last field?
Does this have any special meaning?

[FlagsAttribute]
public enum DependencyPropertyOptions : byte
{
           Default = 1,
           ReadOnly = 2,
           Optional = 4,
           DelegateProperty = 32,
           Metadata = 8,
           NonSerialized = 16,
}
Lance Roberts
  • 22,383
  • 32
  • 112
  • 130
asyncwait
  • 4,457
  • 4
  • 40
  • 53

5 Answers5

72

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
}

By comment request: This info comes straight out of the 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.

ahsteele
  • 26,243
  • 28
  • 134
  • 248
Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • 3
    The compiler works in accordance to the language specification (with, I think, a few exceptions). The language grammar specifies that the extraneous comma is legal in this case (and in a few others such as object initializers, collection initializers and array initializers). Also, unless you know for a fact that the grammar was designed for that reason, it's probably more correct to say that it gives the benefit you list. I wouldn't claim to know the motives of the language committee without first-hand knowledge of such. – jason Jan 27 '10 at 15:04
  • @jason - it makes sense though – asyncwait Jan 27 '10 at 15:17
  • 1
    @Jason To clarify, this isn't my thought, it's actually noted in the C# specification: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-334.pdf (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." – Nick Craver Jan 27 '10 at 15:28
  • Very nice! I did not recall that passage. It would be great if you lifted that into your answer. Plus one from me. – jason Jan 27 '10 at 15:36
  • 1
    Yet that list is not initializing an array, but defining an enum. Is it still considered an array-initializer? Or is there in the standard some other provision specific for enums? – Pablo H Feb 21 '19 at 13:43
  • 2
    Ah, for enums ECMA-334, section 21.1 says at the end: "[Note: C# allows a trailing comma in an enum-body, just like it allows one in an array-initializer (§19.6). end note]". – Pablo H Feb 21 '19 at 13:49
  • My only question is how we can enforce trailing commas in visual studio – NiKiZe Nov 09 '19 at 13:04
15

Also (to Nick Craver post) its much easier to add new enumerations.

This behaviour appropriate not uniquely to enums. Consider following:

var list = new int[] { 1, 2, 3, };
Winston Smith
  • 21,585
  • 10
  • 60
  • 75
Sergey Teplyakov
  • 11,477
  • 34
  • 49
11

One other reason: It makes it easier to code gen.

NotMe
  • 87,343
  • 27
  • 171
  • 245
6

I know that it is an old topic but, another approach that would make sense for this issue is for code versioning systems.

Consider the following example:

//version 1
var myArray = {
    "item 1",
    "item 2"
};
//version 2
var myArray = {
    "item 1",
    "item 2", //will be considered a change, it may be considered an erroneous approach
    "item 3"
}

Now consider this approach:

//version 1
var myArray = {
    "item 1",
    "item 2",
};
//version 2
var myArray = {
    "item 1",
    "item 2", //will not be considered a change, it may be considered an erroneous approach too, but, means that the code wasn't changed intrinsically
    "item 3",
};

Anyhow, both approaches may be considered incorrect or correct depending on the situation. I particularly prefer the second approach that makes much more sense when dealing with code versioning systems.

Anyway hope this helps.

lenilsondc
  • 9,590
  • 2
  • 25
  • 40
4

Why add trailing commas

Why you should take advantage of this feature when writing code manually?

  1. The resultant patches have less lines affected. This makes them easier to read and review.
  2. Automatic merge and conflict resolution are more accurate because there isn't extra noise to confuse the algorithm.

Examples

Without trailing comma (okay)

Example of adding a line when the previous developer didn't leave a trailing comma:

@@ -119,7 +119,8 @@ namespace SomeApp.Example
   {
     NameTitle = contact.NameTitle,
     GivenName = contact.GivenName,
-    FamilyName = contact.FamilyName
+    FamilyName = contact.FamilyName,
+    ProperName = contact.ProperName
   },
   ContactTelephone1 = contact.ContactTelephone1,
   ContactType = contact.ContactType,

Without trailing comma (better)


Example of adding a line when the previous developer left a trailing comma:

@@ -122,2 +122,3 @@ namespace SomeApp.Example
     FamilyName = contact.FamilyName,
+    ProperName = contact.ProperName,
   },

Note there is one line added in the latter vs one removed and two added. This is much easier for human and machine alike to deal with.

Why did they add it to C#?

As for why it is allowed, as per other answers:

The C# Specification (Page 355/Section 17.7) states:

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.


This applies to array initializers, object initializers and enums.

Tim Abell
  • 11,186
  • 8
  • 79
  • 110