Problem
In C#, what is the difference between protected and protected internal modifiers? Their actions appear to be identical.
Asked by Embedd_0913
Solution #1
The access modifier “protected internal” combines the “protected” and “internal” modifiers.
Access Modifiers (C# Programming Guide), MSDN:
protected:
internal:
protected internal:
internal OR ected” (any class in the same assembly, or any derived class – even if it is in a different assembly)
…and for completeness:
private:
public:
private protected:
Answered by M4N
Solution #2
protected can be used by any assembly’s subclasses.
Protected internal is everything that protected is, plus it can be accessed by anyone in the same assembly.
Importantly, it doesn’t mean “subclasses in the same assembly” – it is the union of the two, not the intersection.
Answered by Marc Gravell
Solution #3
The difference is depicted in this table. Protected internal is similar to protected, except it also permits access from other assemblies’ classes.
Answered by Andi AR
Solution #4
In terms of approaches, in practice:
Protected – only inherited classes have access to it; otherwise, it’s private.
internal – only available to classes within the assembly, otherwise private.
Methods that are protected internal – also known as protected or internal – become accessible to inherited classes as well as any other classes in the assembly.
Answered by abatishchev
Solution #5
Even while most people have the definition of “protected internal” accessors correct, there is still a lot of misunderstanding. This clarified the difference between “protected” and “protected internal”:
Inside and outside the assembly, the public is truly public (public internal / public external).
Protected refers to how well the assembly is protected both inside and out (protected internal / protected external) (not allowed on top level classes)
Inside and outside the assembly, private is truly private (private internal / private external) (not allowed on top level classes)
internal is really public inside the assembly but excluded outside the assembly like private (public internal / excluded external)
Inside the assembly, protected internal is truly public, but protected outside the assembly (public internal / protected external) (not allowed on top level classes)
Protected internal is a curious beast, as you can see. It’s counterintuitive.
That begs the question: why didn’t Microsoft build a (protected internal / excluded external), or some type of “private protected” or “internal protected” environment? lol. Does it appear to be missing something?
The fact that you can nest public or protected internal nested members inside protected, internal, or private types adds to the confusion. Why would you want to access a nested “protected internal” within an internal class that isn’t accessible from the outside?
The compiler does not specify that such nested types are constrained by their parent type scope, as Microsoft claims. Protected internals can be compiled into internal classes, limiting the scope to the assembly.
This appears to be an unfinished design to me. They should have reduced the scope of all types to a system that clearly considers inheritance, as well as security and nested type hierarchy. Instead of discovering accessibility of types and members based on an imperfect scoping system, this would have made object sharing incredibly intuitive and granular.
Answered by Stokely
Post is based on https://stackoverflow.com/questions/585859/what-is-the-difference-between-protected-and-protected-internal