What is the use case for the (C# 7.2) "private protected" modifier?

For two-word access modifiers I have this concept - the first accessor is related to another assembly, the second one to that assembly in which it was defined.

protected internal

  • protected in another assembly: accessible only in the child classes.

  • internal in the current assembly: accessible by everyone in the current assembly.

private protected

  • private in another assembly: is not accessible.
  • protected in the current assembly: accessible only in the child classes.

Before C# 7.2 we had protected internal modifier. This really means protected OR internal, that is - member A is accessible to child classes and also to any class in the current assembly, even if that class is not child of class A (so restriction implied by "protected" is relaxed).

private protected really means protected AND internal. That is - member is accessible only to child classes which are in the same assembly, but not to child classes which are outside assembly (so restriction implied by "protected" is narrowed - becomes even more restrictive). That is useful if you build hierarchy of classes in your assembly and do not want any child classes from other assemblies to access certain parts of that hierarchy.

We can take example that Jon Skeet provided in comments. Suppose you have class

public class MyClass {

}

And you want to be able to inherit from it only in current assembly, but do not want to allow to instantiate this class directly except from within this class hierarchy.

Inheriting only within the current assembly may be achieved with internal constructor

public class MyClass {
    internal MyClass() {
    }
}

Preventing direct instantiation except withing current class hierarchy may be achieved with protected constructor:

public class MyClass {
    protected MyClass() {
    }
}

And to get both - you need private protected constructor:

public class MyClass {
    private protected MyClass() {
    }
}