Protected and Internal Examples in c#

Rajesh081725
Posted by Rajesh081725 under C# category on | Points: 40 | Views : 5799
namespace Assembly_1 //This is first assembly.
{
//A normal public class which contains all different types of access-modifier classes in the assembly named 'Assembly_1'
public class Soul
{
//protected class A
protected class A
{
public static void fnA()
{
Console.WriteLine("fnA");
}
}

//internal class
internal class B
{
public static void fnB()
{
Console.WriteLine("fnB");
}
}

//protected internal
protected internal class C
{
public static void fnC()
{
Console.WriteLine("fnC");
}
}

//TIP 1:This class is inside the same class 'Soul' so everything is accessible from above.Hurray!!
class G
{
public static void fnG()
{
//All methods are easily accessible.Does not consider a modified indeed.
A.fnA();
B.fnB();
C.fnC();
Console.WriteLine("fnG");
}
}
}

//Different class but inside the same assembly named 'Assembly_1'
public class Soul_1
{
//Accesibility of 'protected members'::NO
//Accesibility of 'internal members'::YES
//Accesibility of 'protected internal' members::YES
//TIP 2:This class is NOT in same class 'Soul' but in the same assembly so only protected is NOT accessible rest all are accessible.
//IF you want to access protected then inherit class 'Soul'.
public void fnSoul_1()
{
//Soul.A.fnA();//ERROR:Accesibility of 'protected'::NO.ONLY WHEN Soul_1:Soul i.e. when Soul_1 inherits Soul.
Soul.B.fnB();
Soul.C.fnC();
Console.WriteLine("fnSoul_1");
}
}
}

namespace Assembly_2 //This is second assembly.
{
//TIP:if it does not inherit class 'Soul' then we can not access any thing beacuse this 'ANOTHER' assembly.
//TIP:only INTERNAL is NOT accessible , rest all are accessible from first assembly if it inherits class 'Soul'
class KISH : Assembly_1.Soul
{
//=======================================================================
class D
{
void fnD()
{
//
Console.WriteLine("fnD");
A.fnA();//YES, becuase this is 'protected'
//B.fnB();//ERROR:NO, becuase this is 'internal':TIPS 3:only internal is not accessible.
C.fnC();//YES, becuase this is 'protected internal'
}
}
}
}

Comments or Responses

Posted by: T.Saravanan on: 12/13/2012 Level:Silver | Status: [Member] [MVP] | Points: 10
Could you please provide difference b/w Internal and Protected? So that user's can be easily to understand your code.
Kindly post your code inside the code tag.

Login to post response