简体   繁体   中英

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?

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?

You can create an enum with protected access modifier if it's a nested type . Types created directly in a namespace can only assume public and internal access modifiers. It doesn't make sense to create a private , protected , or protected internal type directly in a namespace since you wouldn't be able to use it anywhere (since you can't inherit from a namespace or declare methods in it).

The protected modifier is only meaningful in the context of a class declaration - it establishes that the item marked as protected is accessible to the class an it's derived classes, but not outside of the class.

If your enum is a nested enum of a class, it can be declared as protected.

However, if it is a top-level enum, it can be either public or internal. Public, of course, means it is visible to everyone both inside and outside the assembly. Internal means it is only visible within the assembly. Perhaps internal is what you are looking for.

EDIT: Enum values are not inheritable in C# - the only purpose to allow this would be to be able to add additional values to the enum. While it's not possible with enums, we can use the typesafe enum pattern similar to Java's to do this:

public class BaseEnum 
{
    private readonly int m_Value;

    protected BaseEnum( int val ) { m_Value = val; }

    public static readonly BaseEnum First  = new BaseEnum(1);
    public static readonly BaseEnum Second = new BaseEnum(2);
    public static readonly BaseEnum Third  = new BaseEnum(3);
}

public class DerivedEnum : BaseEnum
{
    protected DerivedEnum( int val ) : base( val ) { }

    public static readonly DerivedEnum Fourth = new DerivedEnum(4);
    public static readonly DerivedEnum Fifth  = new DerivedEnum(5);
}

Are you sure that you are not declaring outside a class?

Inside a class it works fine but you cannot declare a member protected because there is nothing to derive from. If it is defined inside a class it can be derived from and it can be accessed.

A protected enum outside a class definition would not be accessible from anywhere

Nested enum could be 'protected'.

class Test
    {
        protected enum MyEnum
        {
            type1,
            type2
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM