Can someone please explain the difference between the protected and protected internal modifiers in C#? It looks like their behavior is identical.
Can someone please explain the difference between the protected and protected internal modifiers in C#? It looks like their behavior is identical.
The "protected internal" access modifier is a union of both the "protected" and "internal" modifiers.
From MSDN, Access Modifiers (C# Programming Guide):
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.
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:
The type or member can be accessed only by code in the same class or struct.
The type or member can be accessed by any other code in the same assembly or another assembly that references it.
Access is limited to the containing class or types derived from the containing class within the current assembly.
(Available since C# 7.2)
This table shows the difference. protected internal is the same as protected, except it also allows access from other classes in the same assembly.
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.
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.
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
Think about protected internal as applying two access modifier (protected, and internal) on the same field, property or method.
In the real world, imagine we are issuing privilege for people to visit museum:
- Everyone inside the city are allowed to visit museum (internal).
- Everyone outside of the city that their parents live here are allowed to visit museum (protected).
And we can put them together in these way:
Everyone inside the city (internal) and everyone outside of city that their parents live here (protected) are allowed to visit the museum (protected internal).
Programming world:
internal: The field is available everywhere in the assembly (project). It is like saying it is public in its project scope (but can not being accessed outside of project scope even by those classes outside of assembly which inherit from that class). Every instance of that type can see it in that assembly (project scope).
protected: simply means that all derived classes can see it (inside or outside of assembly). For example derived classes can see the field or method inside its methods and constructors using: base.NameOfProtectedInternal.
So, putting these two access modifier together (protected internal), you have something that can being public inside the project, and can be seen by those which have inherited from that class inside their scope.
They can be written in the
internal protected, and does not change the meaning, but it is convenient to write itprotected internal.
This description might be helpful
Internal Member
Internal Member of a class is available or access within the assembly either creating object or in a derived class or you can say it is accessible across all the classes within the assembly.
Protected Member
Protected Member of a class in only available in the contained class (in which it has been declared) and in the derived class within the assembly and also outside the assembly.
Protected Internal
Protected Internal access modifier is combination Protected or Internal.
Protected Internal Member can be available within the entire assembly in which it declared either creating object or by inherited that class. And can be accessible outside the assembly in a derived class only.