abelenky
abelenky

Reputation: 64730

Should C# enums end with a semi-colon?

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?

Upvotes: 28

Views: 4435

Answers (3)

Tim Schmelter
Tim Schmelter

Reputation: 460238

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.

Upvotes: 35

Georgi-it
Georgi-it

Reputation: 3686

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
}

Upvotes: 6

CassOnMars
CassOnMars

Reputation: 6181

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.

Upvotes: 15

Related Questions