none
Difference between visibility and accessibility wrt C# RRS feed

  • Question

  • I have read that accessibility and visibility are different when we consider the IL code .What is it

    and they also said it is related to top level types and nested types in C#

    Wednesday, June 13, 2018 12:22 PM

All replies

  • Accessibility is a language feature and determines what is visible during compilation. For example private members are not visible outside their type. This rule is enforced by the compiler and allows for some separation of implementation from public members. It can be worked around at runtime using reflection or sometimes by simply changing the type the value is accessed as (e.g. explicit interface implementations).

    Visibility is too generic a term to be descriptive. You'd need to quote some resources so we can see the context. IL does expose more accessibility modifiers than C# or other .NET languages. 

    In C# top level types must be public or internal. The other accessibility modifiers do not make sense on a top level type.

    • protected would mean only derived types have access. A derived type cannot be more accessible than its base type so protected doesn't make sense on a root type. 
    • private would mean no one outside the type can access it. But if no one can access the type then it isn't usable and therefore shouldn't exist.
    • protected internal would mean only types in the same assembly and derived types. Given the discussion of protected earlier it doesn't make sense so you're just left with internal which is supported
    • private protected would mean only types that derive from the type in the same assembly. Since protected doesn't make sense nor does private then this accessibility wouldn't either.

    Nested types do have a parent (owner) type. Therefore all the accessibility modifiers make sense here.


    Michael Taylor http://www.michaeltaylorp3.net

    • Proposed as answer by Dolen Zhang Thursday, June 14, 2018 3:00 AM
    Wednesday, June 13, 2018 1:52 PM
    Moderator