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.
- Top 10 Productive C# Videos of 2017
- Quiz: How ref returns work in C# 7.2
- Be careful in using the C# null conditional operators in tests
- Using Static Declaration in C# 6.0
- How to create a Documents Store in Azure Cosmos DB
- Inferred Tuple Element Names
- Azure Functions in C#
- How to configure the C# language version in Visual Studio 2017 projects
- Sequence Equality
- Project References