Access Modifiers

There are still a lot of confusion between access modifiers especially some keywords such as friend, protected, and protected friend. This post is meant to clear things up and distinguish the keywords from each other. Take note that C# keywords are in lower-case (Pascal notation is used below to simply things). In a lot of cases, Private and Public will suffice but there will be instances where you need to limit some exposure to certain types and members -- the logic behind encapsulation.

Public

  • Full accessibility which includes accessibility between different assemblies.
  • Default for enums and interfaces.

Private

  • Accessible only within the containing type.
  • Default for members of classes and structs.

Protected

  • Accessible within the containing type.
  • Accessible by an inheriting type (subclass); but not visible through the base class.
  • Similar to Private in nature but is accessible when the type is inherited.

Friend (VB) / Internal (C#)

  • Accessible anywhere strictly within the same assembly.
  • Default for non-nested types.
Protected Friend (VB) / Protected Internal (C#)
  • Includes both the capabilities of Protected and Friend/Internal.
  • Since Protected and Friend have differentiating restrictions, the two combined opens up doors in terms of accessibility rather than to merge or restrict.
    • For instance: a method is now accessible within the same assembly, and at the same time accessible outside as long as the said type member is inherited.

No Comments