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.
- Public
Public members can be accessed anywhere within or outside of class or assembly by creating an object of that class.
- Private
Private members are restricted to the class and cannot be accessed outside that class even by creating its object.
- 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.
- 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.
- 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.
- 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.
-
- namespace ConsoleApp1
- {
- public class MyClass
- {
- public void GetPublic() { }
- private void GetPrivate() { }
- internal void GetInternal() { }
- protected void GetProtected() { }
- protected internal void GetProtectedInternal() { }
- protected private void GetPrivateProtected() { }
- }
- class YourClass : MyClass
- {
- MyClass mc = new MyClass();
- public void Show()
- {
- mc.GetPublic();
-
- mc.GetInternal();
-
- mc.GetProtectedInternal();
-
- GetPublic();
-
- GetInternal();
- GetProtected();
- GetProtectedInternal();
- GetPrivateProtected();
- }
- }
- }
-
- using ConsoleApp1;
- namespace ClassLibrary1
- {
- public class YourClass : MyClass
- {
- MyClass mc = new MyClass();
- public void show()
- {
- mc.GetPublic();
-
-
-
-
-
- GetPublic();
-
- GetProtected();
-
- GetProtectedInternal();
-
- }
- }
- }
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.