I was trying to understand the differences between protected internal and internal in .NET context by reading the description provided different forums and blogs. I really failed to understand then I created created 2 separate assembly using VS2015. Probably now I have basic understanding. I would like to share with you, it might be helpful to somebody. I tried to use the fields declared in one assembly from another assembly. Also I tried derived from a class that was declared in another assembly.
Here is the code for class1.cs from assembly 1
namespace Z_Dll_1
{
public class PublicBaseClassAssemblyOne
{
internal int _myinternal = 200;
protected internal int _protectedinternal = 100;
protected int _myProtected = 123;
private int _myPrivate = 2;
public int _myPublic = 45;
}
public class DerivedClassAssemblyOne : PublicBaseClassAssemblyOne
{
protected internal int intM = 10;
}
internal class MyInternalClass
{
public void MyMethod()
{
Console.WriteLine("Method one with internal class");
PublicBaseClassAssemblyOne cl1 = new PublicBaseClassAssemblyOne();
cl1._myinternal = 1000; //Internal type is available since it is in same assembly
cl1._protectedinternal = 10; // protected internal is available
cl1._myPublic = 2; // Public OK
//cl1.myPrivate = ?? // nor available since it is private
DerivedClassAssemblyOne drOne = new DerivedClassAssemblyOne();
drOne._myinternal = 30; // Internal and available from derived class
drOne._myPublic = 1; // Public
drOne._protectedinternal = 2; // Able to be accessed from same assembly or derived class from other assembly
}
}
}
Here is code from another assembly, class2.cs
using Z_Dll_1;
namespace Z_Dll_2
{
public class ClassAssembly2
{
public ClassAssembly2()
{
PublicBaseClassAssemblyOne classfromOtherAssembly = new PublicBaseClassAssemblyOne();
classfromOtherAssembly._myPublic = 0; //Only public is available
}
}
public class ClassDerivedFromOtherAssemblyClass : PublicBaseClassAssemblyOne
{
public ClassDerivedFromOtherAssemblyClass()
{
}
void ClassDerivedFromOtherAssemblyClassTestMethod()
{
//_myinternal = 200; // can't access since it was internal to other assembly
_protectedinternal = 100; // this can be accessed as it is derived class from other class that has protected internal
_myProtected = 123; // Ordinary protected data accessed from derived class
//_myPrivate = 2; //Private member can't be accessed from derived class
_myPublic = 45; // Public can be accessed anyway
//Try to create an instance of internal class
//MyInternalClass intClass = new MyInternalClass(); //Not accessible from this assembly
}
}
}