Skip to content

Private protected access modifier C# 7.2correctness

Allow access to members only from derived classes in the same assembly.

Versions of C# prior to 7.2 did not allow the combination of the private and protected access modifiers when defining members of a class.

C# 7.2 permits this ensuring that members defined in this way can only be accessed by derived or containing classes in the same assembly.

Code

C#
class Crc32 {
    // Only this assemblies subclasses can mess with it!
    private protected ulong poly;
}

class ZipCompatibleCrc32 : Crc32 {
    ZipCompatibleCrc32() {
        poly = 0xedb88320u;
    }
}
C#
class Crc32 {
    // Protected would let any derived class anywhere mess with it
    // Internal lets anything in our assembly mess with it
    // Protected Internal lets them both mess with it!
    internal ulong poly;
}

class ZipCompatibleCrc32 : Crc32 {
    ZipCompatibleCrc32() {
        poly = 0xedb88320u;
    }
}

Notes

  • protected internal is not the same as it allows access to derived classes no matter which assembly and any classes within the same assembly.

More information