Which access modifier more accessible, internal or protected?
My understanding of C# access modifiers is as follows:
public: type/type member accessible from any code inside/outside assembly.
protected internal: type/type member accessible from code inside the assembly as well as code inside derived types outside the assembly
protected: type/type member accessible from code within the containing type as well as code inside derived types inside or outside the assembly
internal: type/type member accessible only from code inside the assembly
private: type/type member accessible only from code inside the containing type
The problem is that I am not 100% which one is more accessible, protected or internal. It could be argued that protected is more accessible, since it grants access to some code outside the assembly. But it could also be argued that internal is more accessible, since it grants access to all code inside the assembly.
To make matters worse, remember that a field cannot be more accessible than its type, so if you have something like this:
Code:
public class A
{
protected class B{}
internal B b;
}
You will get a compile-time error message saying that variable b cannot be more accessible than its type B.
By the same token, if you do this:
Code:
public class A
{
internal class B{}
protected B b;
}
You should, contradictorily, get the same compile-time error message, saying that variable b cannot be more accessible than its type B.
On the other hand, you should be able to get away with the following, since variable b is less accessible than its type B.
Code:
public class A
{
protected internal class B{}
protected B b;
}
Regardless of whether or not I got the above code right, I'm pretty sure that there seems to be some ambiguity concerning the relative accessibility of protected vs. internal.
Can someone please explain?