7

According to MSDN here and here (as well as the accepted answer to this qstn), the default accessibility for enums is public. However, this code:

public class Test
{
    enum Color { RED, BLUE, GREEN };
    public void SetColor(Color c) { }
}

will raise this compile error:

Error 1 Inconsistent accessibility: parameter type 'Test.Color' is less accessible than method 'Test.SetColor(Test.Color)'

(which is the same error you get when you set the enum as private.) This error can only be resolved by explicitly modifying enum as public. Is the documentation incorrect?

[I'm compiling with C# 2010 and .NET 4.0.]

Community
  • 1
  • 1
kmote
  • 16,095
  • 11
  • 68
  • 91

5 Answers5

11

That is not true.

The default accessibility for enum types is the same as any other type; internal for top-level types and private for nested types.

The pages you linked to state that the default (and, in fact, only) accessibility level for enum members (Red, Blue, etc) is public.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
5

The mentioned MSDN articles and SO answer refer to "enum member" - i.e. e.g Test.Color.RED, not Test.Color as the enum itself.

Test.Color is a member of class - thus private.

Krizz
  • 11,362
  • 1
  • 30
  • 43
2

That table is referring to the members; the members are "RED", "BLUE" and "GREEEN", and are indeed public literal constants, and accessibility specifies are not permitted.

Contrast, say, to the members of a class (fields, methods, constants, etc); here, as per the table, the default is "private", although you can specify higher accessibility.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
1

I believe that because you're declaring inside the class without a modifier, it assumes to be private as it's the standard behavior in a class. Specify public that should solve the issue. However, note that Code Analysis will recommend this enum be placed outside the class.

Eddie Paz
  • 2,219
  • 16
  • 11
-1

it is because you dont have public, protected, internal on your enum, it takes the default value (which is internal for classes and enums)

sorry for the confusion, you can't make the property public because the enum is private

the public property would be externally public should someone use your program and the compiler tells you about it

Dmitry Savy
  • 1,067
  • 8
  • 22