C# 7.2 introduced new access modifier – ‘Private Protected’ – How it works?

Before Private Protected C# had 5 access modifier such as public, private, protected, internal, protected internal.

As per MSDN

Finally, a new compound access modifier: private protected indicates that a member may be accessed by containing class or derived classes that are declared in the same assembly. While protected internal allows access by derived classes or classes that are in the same assembly, private protected limits access to derived types declared in the same assembly.

This private protected new compound access modifier is used to overcome the limitation of protected internal. In private protected access modifier, the derived class from the different assembly cannot access the Methods, Fields and Properties.

Code Example

Protected Internal in C#

Private Protected in C# – New in C# 7.2

Please check the difference in line number 21 in both the above code.


All Access Modifiers in C#

public: Access is not restricted.

protected: Access is limited to the containing class or types derived from the containing class.

internal: Access is limited to the current assembly.

protected internal: Access is limited to the current assembly or types derived from the containing class.

private: Access is limited to the containing type.

private protected: Access is limited to the containing class or types derived from the containing class within the current assembly.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.