28

In C#, it appears that defining an enum works with or without a semi-colon at the end:

public enum DaysOfWeek
{ Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday} ; //Optional Semicolon?

This C# page from MSDN shows enums ending with semicolons, except for the CarOptions.

I haven't found any definitive reference, and both ways appear to work without compiler warnings.

So should there be a final semicolon or not?

4
  • 4
    (I'm guessing @EricLippert would be in a great position to answer)
    – abelenky
    Jan 29, 2014 at 15:47
  • 1
    "Should" by what standard? It seems to me that it is a completely arbitrary question of style, rather like the question "should the curly brace at the beginning of a type definition appear at the end of the first line or on the next line?", or "should one-line if statements use braces (if (b) x(); else y(); vs if (b) { x(); } else { y(); })?"
    – phoog
    Jan 29, 2014 at 23:45
  • 1
    While I personally dislike semicolons after type declarations (after the final } of a class, interface, enum, or struct), I would prefer an extra comma after the last member inside you enum. That is, I would prefer to end with ... Sunday, }. So my personal preference is: Skip optional semicolons, include all optional commas. Aug 17, 2017 at 7:57
  • 2
    @abelenky: I did not see your comment until four years after the fact. The reason why C and C++ require the semi is discussed here: stackoverflow.com/questions/785686/…. None of those reasons apply to C#. The semicolon was made optional at the end of a class or enum in C# as a courtesy to C++ developers who are in the habit of putting semis at the ends of types. I have never once put a semi at the end of a C# type except when I was testing the parser for spec compliance. I wouldn't worry about it one way or the other. Apr 27, 2018 at 0:12

3 Answers 3

35

From the C# specification (via archive.org):

14.1 Enum declarations

An enum declaration declares a new enum type. An enum declaration begins with the keyword enum, and defines the name, accessibility, underlying type, and members of the enum.

  • attributes opt
  • enum-modifiers opt
  • enum identifier
  • enum-base opt
  • enum-body
  • ; opt

So a single semicolon at the end is allowed but optional.

15

While the C# specification allows for an optional semicolon, the coding guidelines in the rules for StyleCop (SA1106) dictate that if a semicolon is optional, it is not to be used.

2
  • 1
    And yet, most of C# code from MSDN and other reputable sources has the final semicolon.( comment of abalenky on a deleted answer) Jan 29, 2014 at 18:04
  • 7
    This will sound a bit tongue-in-cheek (and it is), but Microsoft isn't exactly known for following standards, including their own.
    – CassOnMars
    Jan 29, 2014 at 19:19
6

Think of the enum as a class. The classes do not need semicolons. The semicolons in the example are most probably put there for the aesthetics. The semicolon is redundant but as we know the compiler does not complaint from such semicolons. For example

public enum MyEnum
{
    a,
    b,
    c
};

This can also be

public enum MyEnum
{
    a,
    b,
    c
}
1
  • 3
    Multiple semicolons at the end of class/enum would result in compile time error, only a single is allowed and ignored.
    – Habib
    Jan 29, 2014 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.