Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Beginners: Access Modifiers Exemplified

0.00/5 (No votes)
25 Jun 2015 1  
Beginners: Access modifiers exemplified

In the recent past, I have been involved in code review where I found a code base which is dominated with “public” access modifiers, and I was like, seriously?. In my recommendations, I had to point that there was lot to be done in terms of code accessibility levels, not everything belonged to the public :).

And therefore in this post, we will be expounding modifiers found in the .NET Framework. Modifiers are used to modify the declaration of types (Interface, Class, Struct) and type members (fields, properties,events, delegates, methods).

In C#, there are quite a number of modifiers, but in this post, we are going to deal with the major access modifiers. Access modifiers specify the accessibility of types and type members. They include:

  1. Private
  2. Internal
  3. Protected
  4. Protected internal
  5. Public

Remember only one access modifier that is allowed in a type except when using “protected internal”. Depending on the context in which a member declaration occurs, only certain declared accessibilities are permitted. If no access modifier is specified in a member declaration, a default access modifier is used.

To bring the point closer home, I would like to think of a family set up where we have a father, mother and children, and the father has a few properties including cars.

We will be using the following classes “Calculator.cs” in assembly “CalculatorLib”.

public class Calculator
 {
   private void Add()
    {
    }

     internal void Subtract()
     {
     }

     protected void Divide()
     {
     }

     protected internal void Multiply()
     {
     }

     public void Modulus()
     {
     }
 }

The following image shows what of “Calculator.cs” is visible when called from “CalculatorClient.cs” class in the same assembly:

Figure 1

Figure 1

The following image shows what of “Calculator.cs” is visible when called from “Program.cs” class in a different assembly:

Figure 2

Figure 2

The following image shows what of “Calculator.cs” is visible when called inside one of its own members:

Figure 3

Figure 3

The following image shows what of “Calculator.cs” is visible when called from “CalculatorDerived.cs” class which is its derived class:

Figure 4

Figure 4

Private

This proves to be the least permissive and restrictive acess modifer. A private member is only visible to the containing type and its members. It's used in class members and nested class. Remember it's not used in top level class.

The private method “Add” can only be accessed by any other member within the class and not outside the class. It’s not even visible to other classes within the same assembly.

Refer to “Figure 3″, you be able to identify “Add” method visible in the containing class and not visible to any of the other figures.

Internal

This level of accessibility can used in top level classes, nested class and their members. They are accessed by the members of the same assembly. The use of internal is in scenarios where you intend components in the same assembly to communicate and share functionalities but as without exposing the functionality to the world (other assemblies).

And so even if you give out your component in terms of a .dll, the internal members are not exposed to the consumer of your library/assembly.

Figure 1, 3, and 4, you are able to see “Subtract” since they are in the same assembly. It’s not visible in Figure 2 since that another assembly.

Protected

This is a member modifier, and can also be used with nested classes. Protected members are only visible to derived classes and the declaring type as well. This modifier is used hand in hand in inheritance as we will come to see later. One other note is that struct cannot be protected as they cannot be inherited.

Visible in Figure 3 since it's in the declaring class and also in Figure 4 since its “CalculatorClient.cs” has derived from “Calculator”.

Protected Internal

Just as the name is suggesting by the combination of the modifiers, it means that every member with this modifier can be assessed by both types in the same assembly as well as derived classes.

Multiply” is visible in Figure 1 because of the “internal” modifier and visible in Figure 4 because of the “protected” modifier.

Public

This is an access modifier for types and type members. Public access is the most permissive access level. There are no restrictions on accessing public members. Anything with public modifier is visible literally everywhere.

“Modulus” which is public is visible anywhere you should be able to see it in every figure above.

Hope this post helps you to use the access modifiers knowledgeably and confidently.

The source code is hosted on github.

Happy coding!!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here