The protected internal keyword combination is a member access modifier. A protected internal member is accessible from the current assembly or from types that are derived from the containing class. For a comparison of protected internal with the other access modifiers, see Accessibility Levels.
internal means that it's only accessible to other classes which are in the same assembly. Public means it's available to all other classes. There is no technical difference between the two.
private - only available to be accessed within the class that defines them. protected - accessible in the class that defines them and in other classes which inherit from that class.
public : accessible from everywhere. protected : accessible by the classes of the same package and the subclasses residing in any package. default (no modifier specified): accessible by the classes of the same package. private : accessible within the same class only.
The "protected internal" access modifier is a union of both the "protected" and "internal" modifiers.
From MSDN, Access Modifiers (C# Programming Guide):
protected:
The type or member can be accessed only by code in the same class or struct, or in a class that is derived from that class.
internal:
The type or member can be accessed by any code in the same assembly, but not from another assembly.
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. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.
Note that: protected internal
means "protected
OR internal
" (any class in the same assembly, or any derived class - even if it is in a different assembly).
...and for completeness:
private:
The type or member can be accessed only by code in the same class or struct.
public:
The type or member can be accessed by any other code in the same assembly or another assembly that references it.
private protected:
Access is limited to the containing class or types derived from the containing class within the current assembly.
(Available since C# 7.2)
protected
can be used by any subclasses from any assembly.
protected internal
is everything that protected
is, plus also anything in the same assembly can access it.
Importantly, it doesn't mean "subclasses in the same assembly" - it is the union of the two, not the intersection.
This table shows the difference. protected internal
is the same as protected
, except it also allows access from other classes in the same assembly.
In practice, about methods:
protected - accessible for inherited classes, otherwise private.
internal - public only for classes inside the assembly, otherwise private.
protected internal - means protected or internal - methods become accessible for inherited classes and for any classes inside the assembly.
There is still a lot of confusion in understanding the scope of "protected internal" accessors, though most have the definition defined correctly. This helped me to understand the confusion between "protected" and "protected internal":
public is really public inside and outside the assembly (public internal / public external)
protected is really protected inside and outside the assembly (protected internal / protected external) (not allowed on top level classes)
private is really private inside and outside the assembly (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)
protected internal is really public inside the assembly but protected outside the assembly (public internal / protected external) (not allowed on top level classes)
As you can see protected internal is a very strange beast. Not intuitive.
That now begs the question why didn't Microsoft create a (protected internal / excluded external), or I guess some kind of "private protected" or "internal protected"? lol. Seems incomplete?
Added to the confusion is the fact you can nest public or protected internal nested members inside protected, internal, or private types. Why would you access a nested "protected internal" inside an internal class that excludes outside assembly access?
Microsoft says such nested types are limited by their parent type scope, but that's not what the compiler says. You can compiled protected internals inside internal classes which should limit scope to just the assembly.
To me this feels like incomplete design. They should have simplified scope of all types to a system that clearly consider inheritance but also security and hierarchy of nested types. This would have made the sharing of objects extremely intuitive and granular rather than discovering accessibility of types and members based on an incomplete scoping system.
protected: the variable or method will be available only to child classes (in any assembly)
protected internal: available to child classes in any assembly and to all the classes within the same assembly
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With