Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

default access modifier for enum in C#

Tags:

c#

oop

enums

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.]

like image 688
kmote Avatar asked Feb 10 '12 23:02

kmote


2 Answers

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.

like image 138
SLaks Avatar answered Nov 11 '22 20:11

SLaks


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.

like image 34
Krizz Avatar answered Nov 11 '22 20:11

Krizz