Well, let's say that you want a class to be only accessed inside her own assembly:
internal class Test
Let's say that you have two classes, one inside the other (nested classes):
protected internal class TestA
{
private TestB _testB;
private class TestB
{
}
public TestA()
{
_testB = new TestB();
}
}
The TestB
class can be only accessed inside methods/properties/contructors inside TestA or inside herself.
The same applies to the protected
modifier.
// Note
If you don't specify the access modifier, by default it will be private
, so on my example the following line:
private TestB _testB;
is equal to
TestB _testB;
And the same applies to the class.
Special Modifier
Then, there is the protected internal
which joins both modifiers so you can only access that class inside the same assembly OR from a class which is derived by this one even if it isn't in the same assembly. Example:
Assembly 1:
public class TestA : TestB
{
public TestB GetBase()
{
return (TestB)this;
}
public int GetA1()
{
return this.a1;
}
}
protected internal class TestB
{
public int a1 = 0;
}
Program
TestA _testA = new TestA(); // OK
TestB _testB = new TestB(); // ERROR
int debugA = new TestA().a1 // ERROR
int debugB = new TestA().GetA1(); // OK
TestB testB_ = new TestA().GetBase(); // ERROR
Source
Link (Access Modifiers)
The type or member can be accessed by any code in the same assembly,
but not from another assembly.
The type or member can be accessed only by code in the same class or
struct.
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 other code in the same
assembly or another assembly that references it.