-2

I thought it's impossible but somehow you CAN add modifier to enum different than public.

This code works perfectly fine and acts as actual private member (inaccesible outside containing class C:

namespace N {
    public class C {
        private enum E { ... }
    }
}

According to @Ben's answer in this question this should be impossible:

Short answer: minimum possible access (cf Jon Skeet's answer).

Long answer:

Non-nested types, enumeration and delegate accessibilities (may only have internal or public accessibility)

                     | Default   | Permitted declared accessibilities
------------------------------------------------------------------
namespace            | public    | none (always implicitly public)

enum                 | public    | none (always implicitly public)

interface            | internal  | public, internal

class                | internal  | public, internal

struct               | internal  | public, internal

delegate             | internal  | public, internal

Nested type and member accessiblities

                     | Default   | Permitted declared accessibilities
------------------------------------------------------------------
namespace            | public    | none (always implicitly public)

enum                 | public    | none (always implicitly public)

interface            | public    | none

class                | private   | All¹

struct               | private   | public, internal, private²

delegate             | private   | All¹

constructor          | private   | All¹

interface member     | public    | none (always implicitly public)

method               | private   | All¹

field                | private   | All¹

user-defined operator| none      | public (must be declared public)

¹ All === public, protected, internal, private, protected internal

² structs cannot inherit from structs or classes (although they can, interfaces), hence protected is not a valid modifier

The accessibility of a nested type depends on its accessibility domain, which is determined by both the declared accessibility of the member and the accessibility domain of the immediately containing type. However, the accessibility domain of a nested type cannot exceed that of the containing type.

Note: CIL also has the provision for protected and internal (as opposed to the existing protected "or" internal), but to my knowledge this is not currently available for use in C#.


See:

http://msdn.microsoft.com/en-us/library/ba0a1yw2.aspx
http://msdn.microsoft.com/en-us/library/ms173121.aspx
http://msdn.microsoft.com/en-us/library/cx03xt0t.aspx (Man I love Microsoft URIs...)

stuartd
  • 70,509
  • 14
  • 132
  • 163
wozar
  • 15
  • 2
  • 4
    Members of the enum cannot have access modifiers applied. From your link: "Enumeration **members** are always public, and no access modifiers can be applied." The enum itself can be made private. –  May 23 '19 at 17:09
  • You can think of the nested enum type as a member of its parent class, and all access modifiers are allowed on members of a class. –  May 23 '19 at 17:13
  • Hi, I rolled back your revision because it removed the context of the question. – stuartd May 23 '19 at 17:22

1 Answers1

6

That's describing the accessibility of members of an enum, not the enum itself. All of the members of the enum can not be given accessibility modifiers, and are always public.

So you can't do this:

public enum Foo
{
    public SomeValue = 1,
    internal AnotherValue = 2,
}

SomeValue and AnotherValue will always be implicitly public.

Servy
  • 202,030
  • 26
  • 332
  • 449