Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enum syntax - extra comma allowed at end?

Tags:

c#

Was surprised to see that a C# Enum definition seems to allow an extra comma at the end (at least in VS2010).

e.g. :

public enum EnumTest1
{
    Abc,
    Def,
}

i.e. there is a comma at the end of "Def". Just wondering if this is allowed by design, or is an oversight. (This might be good to know, because if it is a bug, there may be no guarantees that code like the above will compile in future versions of C#).

like image 830
Moe Sisko Avatar asked Sep 16 '25 22:09

Moe Sisko


1 Answers

It is allowed by design. Similarly, you can have trailing commas in initializers as well. For example:

var ints = new[] { 2, 3, 4, 3, };

var obj = new SomeClass { Prop1 = "foo", Prop2 = "bar", };

I think that allowing trailing commas makes creating auto-generated code much easier because you don't have to add last-in-the-list logic when outputting a list in your code.

like image 51
Jacob Avatar answered Sep 18 '25 12:09

Jacob