Missing last item in listings

I was looking for enumeration options and just noticed a missing comma after the last option. Take, for example, that DayOfWeekenum (click F12to go to the definition):

public enum DayOfWeek
{
    Sunday    = 0,
    Monday    = 1,
    Tuesday   = 2,
    Wednesday = 3,
    Thursday  = 4,
    Friday    = 5,
    Saturday  = 6, // note this comma
}

Is there a reason for this behavior?

+3
source share
3 answers

The trailing comma after the last member enumis optional. This, apparently, was done to help automatically generate code that can just spit things out without thinking about grammar.

Whether a comma is present or not does not change anything.

Take a look at the grammar:

:
  attributes opt enum-modifiersoptenum identifier enum-baseopt enum-body ; opt

:
   : integral-type

:
   { enum-member-declarationsopt}
   { enum-member-declarations , }

- :
  enum-member-declaration
  enum-member-declarations  , enum-member-declaration

-:
  attributes opt identifier
  attributes opt identifier = constant-expression

, enum-body .

+8

, ..:)

( ). , : , : (, , , , ).

+1

The compiler simply ignores the last comma. It should not be there, but it is not a mistake if you put it

0
source

Source: https://habr.com/ru/post/1728039/


All Articles