Before going into access modifiers,one should remember that class by default are private,
if you not specify public modifiers before it.
Below are the access modifiers used in c#.net
1.Private
2.Public
3.Protected
4.Internal
5.Protected internal
1.Public modifiers can be used all the class or other class(within assembly or outside assembly).Simply in human language,getting valuable things from others,without a single penny :)
Ex:
public class A
{
public void Add()
{
}
}
2.Private modifiers can be used local to the class itself.In order to use them public
you have to use property like get,set
Ex:
public class b
{
int a; // default private members..
public int A
{
get
{
return a;
}
set
{
value=a;
}
}
}
Protected,Internal and Protected Internal members are as shown as below:
namespace AbstractDemo
{
public class TestModifiers
{
protected class protectedClassDemo
{
public static void PCD()
{
Console.WriteLine("This is protected class Demo:");
}
}
internal class internalClassDemo
{
public static void ICD()
{
Console.WriteLine("This is internal Class Demo:");
}
}
protected internal class ProtectedInternalClassDemo
{
public static void PICD()
{
Console.WriteLine("This is protected internal class demo:");
}
}
public class TTestModifiers
{
public void GetData()
{
TestModifiers.protectedClassDemo.PCD();
TestModifiers.internalClassDemo.ICD();
TestModifiers.ProtectedInternalClassDemo.PICD();
}
}
}
public class DTestModifiers
{
public void GetDataRead()
{
TestModifiers.internalClassDemo.ICD();
TestModifiers.ProtectedInternalClassDemo.PICD();
}
}
}
In short way:
Protected: Accessible-> within the class defined,but not accessible to different classes
defined in the same namespace and same assembly and accessible to different namespace or different assembly.
Internal:Accessible-> within the class defined and different class defined in same namespace or same assembly but not accessible to different namespace or different assembly.
protected internal:Accessible-> within the class defined and different classes defined
in the same namespace or different namespace or same assembly or different assembly.