Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should C# enums end with a semi-colon?

Tags:

c#

.net

enums

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?

like image 991
abelenky Avatar asked Jan 29 '14 15:01

abelenky


People also ask

Should you still learn C?

In the modern high level languages, the machine level details are hidden from the user, so in order to work with CPU cache, memory, network adapters, learning C programming is a must.

Should I learn C before C++?

There is no need to learn C before learning C++. They are different languages. It is a common misconception that C++ is in some way dependent on C and not a fully specified language on its own. Just because C++ shares a lot of the same syntax and a lot of the same semantics, does not mean you need to learn C first.

Why one should learn C language?

Programming in C forces you to learn memory allocation, data structures and types, how a computer treats and stores different kinds of data, and finally how to manage memory. These are things that you won't get to if you learn only a high-level language.

Should we use C?

C is a general-purpose programming language and can efficiently work on enterprise applications, games, graphics, and applications requiring calculations, etc. C language has a rich library which provides a number of built-in functions. It also offers dynamic memory allocation.


3 Answers

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.

like image 183
Tim Schmelter Avatar answered Sep 21 '22 09:09

Tim Schmelter


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.

like image 30
CassOnMars Avatar answered Sep 19 '22 09:09

CassOnMars


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
}
like image 27
Georgi-it Avatar answered Sep 20 '22 09:09

Georgi-it