Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of the planned "private protected" C# access modifier?

People also ask

What does private protected mean?

The private protected keyword combination is a member access modifier. A private protected member is accessible by types derived from the containing class, but only within its containing assembly. For a comparison of private protected with the other access modifiers, see Accessibility Levels.

What is protected C#?

C# protected is used to allow derived classes access to base class properties and methods with the same name as long as those properties and methods are not private. As with other access modifiers, protected can be used with fields, properties and methods.

What is protected vs private C++?

Private members are accessible within the same class in which they are declared. Protected members are accessible within the same class and within the derived/sub/child class. Private members can also be accessed through the friend function. Protected members cannot be accessed through the friend function.

What are public protected private?

Broadly speaking, public means everyone is allowed to access, private means that only members of the same class are allowed to access, and protected means that members of subclasses are also allowed.


Here are all access modifiers in Venn diagrams, from more limiting to more promiscuous:

private:
enter image description here

private protected: - added in C# 7.2
enter image description here

internal:
enter image description here

protected:
enter image description here

protected internal:
enter image description here

public:
enter image description here


According to "Professional C# 2008" by De Bill Evjen and Jay Glynn, page 1699:

private protected - "only derived types within the current assembly"

C++/CLI has a similar feature - Define and Consume Classes and Structs (C++/CLI) > Member visibility:

private protected -or- protected private - Member is protected inside the assembly but private outside the assembly.


This is just to provide a graph (made with http://ashitani.jp/gv/) of the different accessibility levels (images do not fit in comments).

digraph diagram of C# access levels

Each arrow means "is more restrictive than".

The CLR names are Private, FamilyANDAssembly, Assembly, Family, FamilyORAssembly, Public.


Much later edit: It turned out this nice new access level (with a really poor name) was not eventually included in C# 6.0. It is supported only from C# 7.2 (and I see you updated your question "tags").


It's just a guess, but from a name you could possibly guess it's a more restricted version of protected, (or more relaxed version of private if you wish). And only reasonable variant of it is restricting protected behaviour to assembly.

Possible usage: then you want to have protected for internal implementation, but not for external uses (and you don't want sealing the class).

P.S. It always existed in CLR, but not in C#. It's a combination of protected and internal, quote:

CLR also supports “Family and assembly” access type. This means that the method is accessible from within the declaring type, nested and derived types but only if they’re declared in the same assembly. Well, apparently C# team didn’t think of this as a very useful feature so it’s not supported in this language.


"May be" only visible to subclasses that are in same assembly. This makes it a little restricted than protected.