There are two access modifiers which can easily cause confusions – protected internal and private protected. In this article, we are going to discuss about these two access modifiers.
Access Modifiers
Not a new concept at all. It has been there since the beginning of C#.
Every class, interface, methods, properties, fields have access levels. These access levels decide who can use that respective type or member. The access levels decide whether the members or types can be accessed by other classes, or other assemblies.
These access levels can be specified by using Access Modifiers.
There are various access modifiers available in C#:
- public – no restrictions at all. The
public
type or member can be accessed by all other types regardless of whether they are from same assembly or not. - private – most restricted level. The
private
type or member can be accessed only by sameclass
orstruct
. - protected – The
protected
types or members can be accessed only by code in same class or the class which derives from that class. The derived class can be either in same assembly or it can be in any other assembly. - internal – The
internal
types or members can be accessed by any code in the same assembly. Accessing from any other assembly is not allowed. - protected internal
- private protected (C# 7.2 and later)
The protected internal and private protected are the two access modifiers which we are going to discuss in today’s article.
The “protected internal”
As the name suggests, it is combination of internal
and protected
. This sentence is confusing and can be heard/read at a lot of places.
Myth
Many of us assume that protected internal
is logical AND operation
of two keywords. So the assumption is – because there is internal word in access modifier, this protected member would be accessible only within the assembly which contains it.
But this assumption is NOT correct. It is actually logical OR operation
of these two keywords.
Truth
So, a protected internal
type or member can be accessed:
- by any class which derives from the declaring
class
– regardless of whether the derived class is in same assembly or not - by any class within declaring assembly. The class from same assembly may not be derived from the declaring
class
, but it still can access the member.
It means if the derived class is any other assembly than the assembly which contains protected internal
member, the member would still be accessible.
The “private protected”
This is new access modifier since C# 7.2 – as stated earlier.
It is also a composite access modifier. Unlike protected internal
, it is not union of two other access modifiers.
This access modifier simply means that any private protected member can be accessed from only within the declaring assembly.
It can be accessed:
- either from the same
class
which declares it - OR from any other class from same assembly, which derives from the declaring
class
.
The word private here means private to assembly which contains it.
I hope this short article was helpful. Let me know your thoughts.