Understanding Access Modifiers in C#
Access modifiers defines the accessibility of objects and all of its members, all objects and members have access modifiers implemented, even if it's not stated then default access modifier is applied then.
Access Modifiers Types
There are four access modifier keywords, and combinations of access modifier keywords that we can apply to an object or member such as a field or method.
Private Access Modifier :
Using private keyword we apply private access modifier to an object, those Objects are accessible only inside a structure or a class . As result, it can’t be accessed outside the class/ structure:
Example:
class PersonDetails
{
//Using private keyword
private string name = "DotNetKida";
}
class Program
{
static void Main(string[] args)
{
PersonDetails details
= new PersonDetails();
Console.WriteLine(details.name);
//Error
// '
PersonDetails.name' is inaccessible due to
// its protection/
accessibility level
}
}
|
Internal Access Modifier:
Using internal keyword we apply internal access modifier to an object, those Objects are accessible only inside its own assembly but can not be accessed in other assemblies:
Example:
//First Project
ASSEMBLY
public class DetailClassInFirstAssembly
{
internal string name = "DotNetKida"; //we can access this variable
inside this class
}
class ProgramInFirstAssembly
{
void Print()
{
DetailClassInFirstAssembly obj = new DetailClassInFirstAssembly();
Console.WriteLine(obj.name); // can be access anywhere in this project
}
}
//Second project ASSEMBLY
class Program
{
void Print()
{
DetailClassInFirstAssembly obj2 = new DetailClassInFirstAssembly();
//The Program class in second project can't access the internal
members from another project
Console.WriteLine(obj2.name); // Error. The
name variable can't be access due to its protection level.
}
}
|
Protected Access Modifier:
Using protected keyword we apply protected access modifier to an object,
protected keyword implies that the object is only accessible inside the class and in all classes that derive from that class, we will talk in more details about Inheritance with another article.
Example:
class NameClass
{
//we can access this variable inside this class
protected string name = "DotNetKida";
}
//this is inheritance. ChildClass derives from the NameClass
class
class ChildClass : NameClass
{
void Print()
{
//we can
access it in this class as well because it derives from the NameClass class
Console.WriteLine(name);
}
}
class Program
{
void Print()
{
NameClass objNameClass = new NameClass();
// Error. The name variable is inaccessible due to its
protection level.
Console.WriteLine(objNameClass.name);
// The Program class doesn't derive from the NameClass
}
}
|
Public Access Modifier:
One of the simplest one, the objects that decorated with public keyword they can be access from everywhere no limitations. That's why, there is no accessibility restriction:
Example:
Let's take the last example from protected if we change it protected to public let's see what happened:
class NameClass
{
//we can access this variable inside this class
public string name = "DotNetKida";
}
//this is inheritance. ChildClass derives from the NameClass class
class ChildClass : NameClass
{
void Print()
{
//we can
access it in this class as well because it derives from the NameClass class
Console.WriteLine(name);
}
}
class Program
{
void Print()
{
NameClass objNameClass = new NameClass();
//Now the variable can be access because it is set to public
Console.WriteLine(objNameClass.name);
}
}
|
Protected Internal Access Modifier:
Using protected internal keyword we apply protected internal access modifier to an object,
the protected internal access modifier is a combination of internal and protected. By using this, it can be access (protected internal member) only in the same (project)assembly or in a derived class in other assemblies (projects):
Example:
//First Project (ASSEMBLY)
public class NameClassInFirstProject
{
//we can access this variable inside this class
protected internal string name = "DotNetKida";
}
class ProgramInFirstProject
{
void Print()
{
NameClassInFirstProject obj = new NameClassInFirstProject();
// This is
OK. Anywhere in this project (assembly) we can access the name variable.
Console.WriteLine(obj.name);
}
}
//Second project (ASSEMBLY)
class Program : NameClassInFirstProject //Inheritance
{
void Print()
{
//This is OK as well. The class Program derives from the
NameClassInFirstProject .
Console.WriteLine(name);
}
}
|
Private Protected Access Modifier:
Using private protected keyword we apply private protected access modifier to an object.
The private protected access modifier is a combination of the private and protected keywords. We can access members inside the containing class or in a class that derives from a containing class, but only in the same assembly. If we try to access it from another assembly, we will get an error.
Example:
// Assembly1.cs
public class ParentClass
{
private protected int
myNewValue = 0;
}
public class ChildClass:
{
void Access()
{
var baseObject = new ParentClass();
// Error CS1540, because myNewValue can only be accessed by
// classes
derived from ParentClass.
// baseObject.myNewValue = 5;
// OK, accessed through the current derived class instance
myNewValue = 5;
}
}
// Assembly2.cs
class ChildClass2:
{
void Access()
{
// Error CS0122, because myNewValue can only be
// accessed by types in Assembly1
// myNewValue = 10;
}
}
|
Conclusion
I hope it will help you to understand access modifiers in C#. Now we have learned what types of access modifier can be use in C# and limitations of them.
References :
0 Comments