Default access modifier for listing in C #

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 compilation error:

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

(This is the same error that you get when setting an enumeration as private .) This error can only be resolved by explicitly changing the enumeration as public . Invalid documentation?

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

+6
source share
5 answers

This is not true.

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

The pages you linked to indicate that the default accessibility level (and, in fact, only) for enumeration members ( Red , Blue , etc.) is publicly available.

+10
source

The indicated MSDN articles and SO response refer to an enumeration element - for example, Test.Color.RED , and not Test.Color as the enumeration itself.

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

+4
source

This table refers to members ; the members "RED", "BLUE" and "GREEEN" are indeed public literal constants, and accessibility specifications are not permitted.

Contrast, say, class members (fields, methods, constants, etc.); here, according to the table, "private" is used by default, although you can specify a higher availability.

+2
source

I believe that since you declare inside a class without a modifier, it assumes that it is private, like standard behavior in a class. Indicate the publication that should solve the problem. However, note that Code Analysis will recommend that this enumeration be placed outside the class.

+1
source

this is because you do not have a public, protected, internal for your enumeration, it takes a default value (which is internal for classes and enumerations)

Sorry for the confusion, you cannot make the property public, because the enumeration is private

the public property will be externally public if someone uses your program and the compiler tells you about it

-1
source

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


All Articles