Class declaration in C #

1

At what other levels of access can I declare a class in C # in addition to public and private . I found these levels in the Microsoft site :

protected
internal
protected internal
    
asked by anonymous 16.09.2017 / 15:26

1 answer

2

Classes can be declared as public , private , or internal implicitly if nothing is described in the code.

Contrary to what many people think a class without explicit visibility is not public, it is internal to the compilation unit, in this case to the assembly file, so everything that is compiled together in the same file executable to see, but other parts of the application does not, to make public need to make explicit it.

Private class only makes sense if it is inside another class which will only make this class see it. Its usefulness is limited for some cases.

The documentation exposed in the question does not talk about class visibility, but about class members. Perhaps confusing these concepts is difficult to understand.

Actually when I say classes I'm actually meaning types , I used the term of the question, but everything that goes for classes is valid for structures, enumerations and delegates.

If you want to know about member visibility the question is not exactly duplicate because C # is a little different.

There are also public members ( public ) that are seen by every application but with a type or instance scope of that type.

It has private members ( private ) that are only seen within the type, whether static or not.

The protected member ( protected ) already known is private, but also lets inherit classes of this class see this member. Note that only classes can have the member protected because it is the only one that accepts inheritance. Obviously, sealed classes can not benefit from protected since they will never be inherited.

The member internal is seen by the entire compilation unit (the assembly ), it is semi-public.

There is also proteced internal which is a mixture of the two, so the member can be seen by every compilation unit and by the classes that inherit from it.

There is a proposal to have the member that can be accessed from an inherited class as long as it is in the same assembly . We'll see if it goes into future versions.

    
17.09.2017 / 17:05