Private Protected Access Modifier In C#

In December 2017, Microsoft released C# version 7.2 and within that, it introduced a new compound access modifier - Private Protected. While the existing modifiers have become very common across the C# developer community, there is still confusion going on about private protected. Let's take a closer look at all the access modifiers available in C# along with their behavior.

Access Modifiers

Access Modifiers are special keywords used in a C# program to specify the scope of the members of the class. Each access modifier has its own behavior which is used to achieve a very important principle of OOP; i.e., encapsulation.

Introduction to Private Protected in C#

With the addition of a new compound access modifier in C# 7.2, the count of access modifiers available in C# goes to six. 

  1. Public
    Public members can be accessed anywhere within or outside of class or assembly by creating an object of that class.
  1. Private
    Private members are restricted to the class and cannot be accessed outside that class even by creating its object.
  1. Protected
    Protected members can be accessed within that class or in the class that is deriving it. But protected members can never be accessed by creating an object of the class. Protected members can be accessed only through inheritance.
  1. Internal
    Internal members of a class can be accessed anywhere within the assembly in which the class resides. We can think of internal as a subset of public, i.e., internal members act as public but only in that assembly. They are not accessible outside of that assembly.
  1. Protected Internal
    This compound access modifier is a combination of both Protected and Internal. Class members with this access modifier can be accessed in a derived class through inheritance or by creating an object or base class. But in a different assembly, this can be accessed only through inheritance and not by creating an object.
  1. Private Protected
    This compound access modifier is introduced to overcome the limitation of Protected Internal. In a private protected access modifier, the derived class from the different assembly cannot access the members which are Private Protected.

If any of the above explanations is confusing to you, don't worry. I have demonstrated the behavior of all of the access modifiers through the code below.

How to use Private Protected?

Please keep in mind that private protected requires version 7.2 of C#. To accomplish this,

  • Right-click on your project
  • At the bottom of the available options click Properties
  • Click on Build
  • Click on Advanced
  • Within the pop-up, change the language version to match the required version (7.2)

 Now, let’s take a look at the code.

  1. // Inside the same assembly  
  2. namespace ConsoleApp1  
  3. {  
  4.     public class MyClass  
  5.     {  
  6.         public void GetPublic() { }  
  7.         private void GetPrivate() { }     
  8.         internal void GetInternal() { }  
  9.         protected void GetProtected() { }  
  10.         protected internal void GetProtectedInternal() { }  
  11.         protected private void GetPrivateProtected() { }  
  12.     }  
  13.     class YourClass : MyClass  
  14.     {  
  15.         MyClass mc = new MyClass();  
  16.         public void Show()  
  17.         {  
  18.             mc.GetPublic();  
  19.     //mc.GetPrivate(); Not accessible as private members of a class are not accessible outside class.  
  20.             mc.GetInternal();  
  21.     //mc.GetProtected(); Not accessible as protected members can be accessed only through inheritance and not by creating an object.  
  22.             mc.GetProtectedInternal();  
  23.     //mc.GetPrivateProtected(); Not accessible as Private Protected members can be accessed only through inheritance in same assembly.                
  24.             GetPublic();  
  25.     //GetPrivate(); Not accessible as private members of a class are not accessible outside class.  
  26.             GetInternal();  
  27.             GetProtected();  
  28.             GetProtectedInternal();  
  29.             GetPrivateProtected();  
  30.         }  
  31.     }  
  32. }  
  33. //Outside the assembly  
  34. using ConsoleApp1;  
  35. namespace ClassLibrary1  
  36. {  
  37.     public class YourClass : MyClass  
  38.     {  
  39.         MyClass mc = new MyClass();  
  40.         public void show()  
  41.         {  
  42.             mc.GetPublic();  
  43.     //mc.GetPrivate(); Not accessible as private members of a class are not accessible outside class.  
  44.     //mc.GetInternal(); Not accessible as internal members are not accessible outside it's assembly.  
  45.     //mc.GetProtected(); Not accessible as protected members can be accessed only through inheritance and not by creating an object.  
  46.     //mc.GetProtectedInternal(); Not accessible as Protected Internal members can not be accessed outside of assembly by creating an object.  
  47.     //mc.GetPrivateProtected(); Not accessible as Private Protected members are not accessible outside of assembly by creating an object or through inheritance.      
  48.     GetPublic();  
  49.     //GetPrivate(); Not accessible as private members of a class are not accessible outside class.  
  50.             GetProtected();  
  51.     //GetInternal(); Not accessible as internal members are not accessible outside it's assembly.  
  52.             GetProtectedInternal();  
  53.     //GetPrivateProtected(); Not accessible as Private Protected members are not accessible outside of assembly by creating an object or through inheritance.    
  54.         }  
  55.     }  
  56. }  

Conclusion

I hope I was able to demonstrate the behavior of all access modifiers along with Private Protected and this article will help you for better understanding of the concept. Your feedback is always welcomed.


Similar Articles