Enums are one of the important concepts of C#.Net programming language, in this C#.Net tutorial, we will discuss, C#.NET Enum Naming Conventions with a few examples.
Enums are a distinct type consisting of a set of named constants called the enumerator list. They are used to assign names to the integral constants which make a program easy to read and maintain.
Example:
public enum Days
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
Enums Naming Conventions in C#.Net
When it comes to naming enums in C#.Net, there are several best practices that you should follow.
1. Use Pascal Case
In C#, the standard convention for most types — including enums — is to use Pascal Case. This means that the first letter of the identifier and the first letter of each subsequent concatenated word are capitalized.
Example:
public enum DayOfWeek
{
Sunday,
Monday,
//...
}
2. Use Singular Names if Possible
As a general rule, if the enum’s values are not bit fields, you should use a singular name for the enum type.
Example:
public enum Color
{
Red,
Green,
Blue,
Yellow
}
3. Use Plural Names for Flags Enum
For enums that represent bit fields and can therefore hold multiple values (by applying the Flags
attribute), use a plural name.
Example:
[Flags]
public enum FileAccess
{
None = 0,
Read = 1,
Write = 2,
ReadWrite = Read | Write
}
4. Do Not Prefix the Enum Type Name
Avoid prefixing the enum type name with “E” or any other letter.
Incorrect:
public enum ESeason
{
Spring,
Summer,
Autumn,
Winter
}
Correct:
public enum Season
{
Spring,
Summer,
Autumn,
Winter
}
5. Do Not Prefix Enum Member Values
You should not prefix enum member values with the enum type name because when accessing the member value, the type name is always prefixed.
Incorrect:
public enum Season
{
SeasonSpring,
SeasonSummer,
SeasonAutumn,
SeasonWinter
}
Correct:
public enum Season
{
Spring,
Summer,
Autumn,
Winter
}
6. Avoid Creating Enums with Only One Value
Enums should represent a group of related constants, and it doesn’t make sense to have an enum with only one value.
Incorrect:
public enum Direction
{
North
}
Correct:
public enum Direction
{
North,
East,
South,
West
}
7. Enum Member Naming Convention
Just as it’s important to follow best practices when naming your enums, it’s equally important to follow best practices when naming the members of those enums.
Enum members should follow the same naming conventions as constants, i.e., use Pascal Case. Like enum type names, enum members should also be as descriptive as possible, while still being succinct.
Avoid using abbreviations and ensure that the name clearly represents the value it holds. Furthermore, enum members should be unique within the same enum set.
For example:
public enum ErrorCode
{
None,
Unknown,
InvalidParameter,
Timeout
}
In the above example, each member of the ErrorCode
enum is clear, concise, and unique, making it easy to understand what each value represents.
C#.net enum naming convention uppercase
While it’s generally accepted in the C# community to use Pascal Case for enums and their members, some developers might use uppercase for enum members, especially if these enum members are considered as named constants. In such cases, underscores are used to separate the words.
Let’s look at an example:
public enum StatusCode
{
OK,
NOT_FOUND,
INTERNAL_SERVER_ERROR
}
This style is commonly found in other programming languages, particularly C and C++.
However, this is not a widely accepted practice in C#. The official Microsoft C# naming convention guidelines recommend using Pascal Case, not uppercase with underscores, for both enum type names and enum members.
Here is the more conventional, PascalCase version of the same enum:
public enum StatusCode
{
Ok,
NotFound,
InternalServerError
}
The latter is more in line with C# naming conventions and is generally preferred when writing idiomatic C# code. However, the most important thing is to maintain consistency across your codebase. If you choose to use uppercase with underscores for your enum members, make sure to use it consistently throughout your code.
Conclusion
Enums in C# are a powerful tool to enhance the readability and maintainability of your code. By following the above-mentioned naming conventions, you can ensure that your enums are clear, concise, and understandable to others working on the same codebase.
Now, you can implement these C#.NET Enum Naming Conventions while working with enums in C#.Net. It also covers the c# enum naming convention uppercase.
You may also like:
- Combobox naming convention in c#.net with examples
- C#.Net Fields Naming Conventions
- Naming Convention in C# with Examples
Bijay Kumar is a renowned software engineer, accomplished author, and distinguished Microsoft Most Valuable Professional (MVP) specializing in SharePoint. With a rich professional background spanning over 15 years, Bijay has established himself as an authority in the field of information technology. He possesses unparalleled expertise in multiple programming languages and technologies such as ASP.NET, ASP.NET MVC, C#.NET, and SharePoint, which has enabled him to develop innovative and cutting-edge solutions for clients across the globe. Read more…