Can you specify an access modifier for an enumeration?
Short answer
Yes, you can specify an access modifier for an enum declared at the top level, such as public or internal, following the same accessibility rules that apply to classes, and by default a top level enum without an explicit modifier is internal.
Advertisement
AModel answer
Yes, you can specify an access modifier for an enum declared at the top level, such as public or internal, following the same accessibility rules that apply to classes, and by default a top level enum without an explicit modifier is internal. However, the individual members inside an enum, such as its named constant values, cannot have their own separate access modifiers, since they always share the same accessibility as the enum itself.
public enum OrderStatus {
Pending, Shipped, Delivered
}
internal enum InternalFlags {
None, Active, Archived
}Real-world example
A shared library exposes its OrderStatus enum as public so that any application referencing the library can use these status values, while keeping an internal enum used only for internal flags hidden from external consumers.
Likely follow-ups
- What is the default accessibility of an enum if no modifier is specified?
- Can an enum be nested inside a class and given a different accessibility than the outer class?
Related topics