14

C# 7.2 introduced the private protected modifier, whats the difference to internal protected?

From the doc:

A private protected member is accessible by types derived from the containing class, but only within its containing assembly.

Isn't that exactly what internal protected does?

Enes Sadık Özbek
  • 993
  • 10
  • 17

1 Answers1

25

From Access Modifiers (C# Programming Guide)

Protected Internal : The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly.

And

Private Protected : The type or member can be accessed only within its declaring assembly, by code in the same class or in a type that is derived from that class.

Another useful link C# 7 Series, Part 5: Private Protected

Ehsan Ullah Nazir
  • 1,827
  • 1
  • 14
  • 20
  • The second quote from the Microsoft docs seems ambiguous. I was thinking it meant a private protected type/member can be accessed within its declaring assembly OR in the same class OR in a type derived from that class, which is the same as protected internal. I think replacing the "by" with "by either" would resolve this. – Ruben9922 Oct 24 '19 at 09:28
  • 11
    I would remember it as: **Protected Internal = Protected OR Internal** - can be accessed by any code in the assembly OR from within a derived class in another assembly. **Private Protected = Protected AND Internal** - can be accessed in the assembly AND from within same/derived class. – Demonel May 05 '21 at 09:48