[WEBINAR] Bulletproof: How Today's Business Leaders Stay on Top

Access Modifiers

Definition - What does Access Modifiers mean?

Access modifiers are keywords used to specify the accessibility of a class (or type) and its members. These modifiers can be used from code inside or outside the current application.

Access modifiers in .NET are used to control the accessibility of each of the members of a type from different possible areas of code. This can be handled from within the current assembly or outside it. An assembly represents a logical unit of functionality and consists of types and resources located in one or more files.

The purpose of using access modifiers is to implement encapsulation, which separates the interface of a type from its implementation. With this, the following benefits can be derived:

  • Prevention of access to the internal data set by users to invalid state.
  • Provision for changes to internal implementation of the types without affecting the components using it.
  • Reduction in complexity of the system by reducing the interdependencies between software components.

Techopedia explains Access Modifiers

The .NET framework provides an option of having five types of access modifiers:

  1. Private – code within the type can only access the members of that type, and hence accessibility is limited to current type
  2. Public – code from anywhere within the current assembly, or another assembly that references it, can access the members of the type, and hence allows accessibility from anywhere
  3. Protected – code within the type, or its derived classes, can access the members of the type and hence accessibility is limited to current type and derived classes
  4. Internal – code in the current assembly, but not from another assembly, can access the members of the type, hence accessibility is limited to current assembly
  5. Protected Internal – code in the current assembly can access the members of the type and also from the assembly that references it. Hence, accessibility is from derived classes in the current assembly, and must take place through an instance of derived class type in the assembly referencing it

There are several rules that apply to the access modifiers:

  • When there is no access modifier specified to type members, the default access level is private and internal.
  • There are no access modifiers allowed for namespaces, since they are public.
  • The nested classes and struct members declared within a type are, to the containing class, private by default.
  • Struct members cannot be declared protected since it does not support inheritance.
  • Destructors cannot have access modifiers.
  • Derived type cannot have greater accessibility than its base type.
  • The member of a containing type should have accessibility lesser than that of its containing type. This can be illustrated with an example: A public method in a containing type cannot have “A” as a parameter, if type A is not in public visibility.
  • Interfaces are declared public and internal, and cannot have other access modifiers, since interfaces are mainly used for access by classes to derive from it.
  • Access modifiers are used not only to class members, but also to other code constructs with the same intention.

Share this:

Connect with us

Email Newsletter

Join thousands of others with our weekly newsletter

The 4th Era of IT Infrastructure: Superconverged Systems
The 4th Era of IT Infrastructure: Superconverged Systems:
Learn the benefits and limitations of the 3 generations of IT infrastructure – siloed, converged and hyperconverged – and discover how the 4th...
Approaches and Benefits of Network Virtualization
Approaches and Benefits of Network Virtualization:
Businesses today aspire to achieve a software-defined datacenter (SDDC) to enhance business agility and reduce operational complexity. However, the...
Free E-Book: Public Cloud Guide
Free E-Book: Public Cloud Guide:
This white paper is for leaders of Operations, Engineering, or Infrastructure teams who are creating or executing an IT roadmap.
Free Tool: Virtual Health Monitor
Free Tool: Virtual Health Monitor:
Virtual Health Monitor is a free virtualization monitoring and reporting tool for VMware, Hyper-V, RHEV, and XenServer environments.
Free 30 Day Trial – Turbonomic
Free 30 Day Trial – Turbonomic:
Turbonomic delivers an autonomic platform where virtual and cloud environments self-manage in real-time to assure application performance.