Introduction
Access modifiers are the defined level of permission to access properties and methods. By declaring these access modifiers, we are defining a variable or an event can be accessed from assembly to within that class. Let's see how.
Description
There are 4 major access modifiers in C#. These are:
Public
Private
Protected
Internal
Protected Internal
Public
Using Public
, an event or a variable can be accessed from outside of the class, where it belongs. And also from the outside of the assembly.
class ClassTest
{
public void MethodPublic()
{
}
}
class Program
{
static void Main(string[] args)
{
ClassTest objClassTest = new ClassTest();
objClassTest.MethodPublic(); }
}
Private
It restricts the use of methods and variables only within the class itself. It can't be used from outside of the class. As you declare a private
constructor of a class, that class can't be accessed from outside that class, you can't create an object of that class,
Example 1: Private keyword
class ClassTest
{
private void MethodPrivate()
{
}
}
class Program
{
static void Main(string[] args)
{
ClassTest objClassTest = new ClassTest();
objClassTest.MethodPrivate(); }
}
Example 2: Private Constructor
class ClassTest
{
private ClassTest() { } }
class Program
{
static void Main(string[] args)
{
ClassTest objClassTest = new ClassTest();
}
}
Protected
This allows variables and methods to access from that class and the sub class of the class. That means that methods can be accessed within that class and from the classes, which actually inherit that class.
class ClassTest
{
protected int _a;
}
class ClassTest2 : ClassTest
{
ClassTest2()
{
this._a = 10; }
}
class ClassTest3
{
ClassTest3()
{
this._a = 10; }
}
Internal
Internal
is introduced in C#. In JAVA, we don't have this access modifier. This allows the access after Protected
. As Protected
, it also allows to access the methods and variables from that class and the sub classes of that class. It added the assembly into it. That means the variables and methods can be accessed within the assembly where the class belongs. Now make sure that here we are talking about Namespace
, because Namespace
and assembly are slightly different. An assembly can hold more than one Namespace
. Assemblies are actually the DLL of the project.
class ClassTest
{
internal void MethodInternal()
{
}
}
class Program
{
static void Main(string[] args)
{
ClassTest objClassTest = new ClassTest();
objClassTest.MethodInternal(); }
}
Protected Internal
Protected Internal
allows you to access the variables and methods to access from that class and sub classes of that class. Also allows to access within the same assembly. This means in protected
, if the class is inheriting the super class and the method or variable is protected, then the assembly doesn't matter to access. But in the Internal
, the assembly matters if the class is inheriting the super class. That is why we use Protected Internal
access modifier.
class ClassTest
{
protected internal string name; public void print()
{
Console.WriteLine("\nMy name is " + name);
}
}
class Program
{
static void Main(string[] args)
{
ClassTest objClassTest = new ClassTest();
objClassTest.name = "Arka";
objClassTest.print();
}
}
In short:
Public
: Anywhere from the class
Private
: Only within the class
Protected
: Only within that class and the sub classes of that class
Internal
: Within the assembly of the class
Protected Internal
: Within that class, sub classes of that class and and assembly
Hope it will clear your concept about the access modifiers. Don't forget to post your valuable comments. CodeProject