Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Enumeration allows comma in the last field

Tags:

c#

.net

enums

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, } 
like image 980
asyncwait Avatar asked Jan 27 '10 13:01

asyncwait


2 Answers

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.

like image 131
Nick Craver Avatar answered Sep 19 '22 13:09

Nick Craver


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, }; 
like image 45
Sergey Teplyakov Avatar answered Sep 19 '22 13:09

Sergey Teplyakov