Why and how does C# allow accessing private variables outside the class itself when it’s within the same containing class?

See section 3.5.1 of the C# language specification. The relevant text is this:

Private, which is selected by
including a private modifier in the
member declaration. The intuitive
meaning of private is “access limited
to the containing type”.

Note that the modifier is relevant to the type, not the instance.

And then further in section 3.5.2 some rules are further explained:

In intuitive terms, when a type or
member M is accessed, the following
steps are evaluated to ensure that the
access is permitted:

  • First, if M is declared within a type (as opposed to a compilation unit
    or a namespace), a compile-time error
    occurs if that type is not accessible.
  • Then, if M is public, the access is permitted.
  • Otherwise, if M is protected internal, the access is permitted if
    it occurs within the program in which
    M is declared, or if it occurs within
    a class derived from the class in
    which M is declared and takes place
    through the derived class type
    (§3.5.3).
  • Otherwise, if M is protected, the access is permitted if it occurs
    within the class in which M is
    declared, or if it occurs within a
    class derived from the class in which
    M is declared and takes place through
    the derived class type (§3.5.3).
  • Otherwise, if M is internal, the access is permitted if it occurs
    within the program in which M is
    declared.
  • Otherwise, if M is private, the access is permitted if it occurs
    within the type in which M is
    declared.
  • Otherwise, the type or member is inaccessible, and a compile-time error
    occurs.

Leave a Comment