Secure enumeration is not possible in C #

I just want to understand why I cannot create a secure enumeration in C #?

The compiler refuses to accept that

Does anyone know why this could be?

+3
source share
5 answers

You can have a protected type that is nested in another type - and includes enumerations:

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

However, it makes no sense to protect a non-nested type — a protected modifier is the provision of access to a member from a derived type; since the top level type is only a member of the namespace, and not of another type, there is no type from which you could get additional access.

, , , ?

+12

enum protected , . , namespace, public internal. private, protected protected internal namespace, ( ).

+6

protected - , , , , .

, .

, , , . , , , , . , . , - , .

EDIT: Enum # - , , . , typeafe enum pattern, Java, :

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);
}
+3

, ?

Inside the class, it works fine, but you cannot declare a member protected, because nothing can be deduced from it. If it is defined inside a class, it can be obtained from and can be accessed.

Protected enumeration outside the class definition will not be available anywhere

+1
source

A nested enumeration can be "protected."

class Test
    {
        protected enum MyEnum
        {
            type1,
            type2
        }
    }
+1
source

Source: https://habr.com/ru/post/1736884/


All Articles