March 8

Access Modifiers in C# and Private Protected

2  comments

Learn about the new private protected access modifier in C# 7.2 and how it relates to all the other access modifiers.

Video Link: https://www.youtube.com/watch?v=KTk_XNQMPr4

Transcript Of The Video

In this video I want to talk about the private protected member access modifier that has been introduced in C# 7.2. In order to do so, I'd like to have a quick look at all the other access modifiers to better understand how the new modifier works. I have a solution in Visual Studio with two projects: one console application and one library that is actually referenced by the console application. The first modifier we are all familiar with is Public. When I declare a member as Public, means that everyone can access it, inside the assembly or outside. If you make a member Private, you can't change it outside the context of the class. The next access modifier is Protected. Now all the types that derive from Shape are able to access the member. However, you are not able to create an instance of a shape and change the member. Only the classes themselves are able to change the member. The other modifier is Internal. This means that this member can be accessed everywhere inside the assembly where it belongs. However, the member is inaccessible outside the assembly boundary. The next level is Protected Internal. It's the combination of the previous two using OR. It is Protected or Internal. The member can be accessed everywhere inside the assembly where is defined and also can be accessed in all derived types regardless in which assembly they lives. In C# 7.2 a new modifier has been added and it is called Private Protected. It gives access to a particular member only to derived types inside the assembly where is defined. Everywhere else is inaccessible. It is the combination of Protected and Internal. There was no way to do this before.  This is a new modifier that gives you an additional level of power in defining how you want to encapsulate your members. This access modifier was already available in the CLR so no changes were required in the CLR to implement this feature. It was never been implemented in C# mostly because it was quite difficult to name. It can creates a bit of a confusion. The best way to see it is: Private Protected is Protected And Internal while Protected Internal is Protected Or Internal.

Previous Posts


Tags

.NET, C#, C# 7.2, Visual Studio


You may also like

The best way to stay up to date with C# 10 features

My Technical Journey from my first program to Lead Software Engineer

Top 10 C# Developer News from Microsoft Build 2020

Agile Estimation

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

    Subscribe to our newsletter now!

    Get instant access to the Master C# 9 webinar to learn all the new exciting C# 9 features quickly.
    Get regular videos and news on C# and .NET and offers on products and services to master C#.

    We will collect, use and protect your data in accordance with our Privacy Policy.

    >