Search code examples
c#language-features

Protected Enum Not Possible In C#


I just want to understand why I cannot create a protected enum on C#?

The compiler refuses to accept that

Does anyone know why that may be?


Solution

  • You can have a protected type which is nested within another type - and that includes enums:

    public class Outer
    {
        protected enum NestedEnum { Foo, Bar, Baz };
    }
    

    However, it doesn't make sense to make a non-nested type protected - the protected modifier is about granting access to a member from within a derived type; as a top level type is only a member of a namespace rather than another type, there's no type you could derive from to get the extra access.

    Could you describe what you're actually trying to achieve, and then we could try to work out what the most appropriate visibility would be?