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?
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.
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.
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.
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.
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.
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.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With