Access Modifiers are used for providing the access scope of any class or members of classes or any other types in C#. Scope means
at what places we can access these members in any assembly or other referenced assemblies.
In short we can say that it provides the accessibility level of any types in C#. Accessibility means whether these members
are able to access in code from anywhere in assembly or outside the assembly or they have some limitations. There are five types
of access modifers are mainly used in C# which are listed below.
Public Modifiers are used with Public Keyword . There is no any limitation for public modifiers in application to access it . In simple words we can say that we can access publicly decalred code anywhere in assembly or outside the assembly referenced in any other projects. Below is the screen images for Publicly decalred Classes with examples.
public class ExampleClassA
{
public int StudentID { get; set; }
public string Name { get; set; }=string.Empty;
private int Salary { get; set; }
}
In the above example I have created a public class and named it ExampleClassA and inside this class I have declared three members , among them two is public and one is private so we can access this public class anywhere in code but only public members are accessed as shown in below code.
public class ChildClassB
{
public ChildClassB()
{
ExampleClassA exampleClassA = new ExampleClassA();
// exampleClassA.Salary = 23000; Commented it because we can not access private members here
exampleClassA.StudentID = 1000;
exampleClassA.Name = "Bill Formhals";
}
}
In the above example I have created a another public class and named it ChildClassB and inside this class I have declared a constructor and inside the constructor I have created an object of public class ExampleClassA and assigns the values to class members , among them two is public so that we are able to access them and assign them but we are not able to access third one that is private , so inshort we can access this public class anywhere in code but only public members are accessed and when are trying to access the private members it will throw a compile time error as you can shown in below image.
As you can see in above image only public members are shown in accessible list with object like StudentID and Name is visible in the list but Salary is not accessible due to private access specifiers only.
Private Modifiers are used with Private Keyword . There is strict limitations for Private modifiers in application to access it . We can access Private members only in same class where it is declared. In simple words we can say that we can not access privately decalred code anywhere in assembly or outside the assembly referenced in any other projects. Below is the screen images for Privately decalred Classes with examples.
class ExampleClassA
{
int StudentID { get; set; }
public string Name { get; set; }=string.Empty;
int Salary { get; set; }
private void PrintStudentDeatils()
{
StudentID = 1000;
Name = "Rocky";
Salary = 52000;
Console.WriteLine("Student Details are :" +"Student ID is "+StudentID+ "Name is :-" +Name+"Salary is :-"+Salary);
}
}
In the above example I have created a Private class and named it ExampleClassA and inside this class I have declared four members , among them three is Private and one is public so we can not access this private class anywhere in code but only access private members are inside the same class.
public class ChildClassB
{
public ChildClassB()
{
//Can not access below code in this class because Private class is not accessible to outside
Commented it because we can not access private members here
ExampleClassA exampleClassA = new ExampleClassA();
Commented it because we can not access private members here
// exampleClassA.Salary = 23000;
Commented it because we can not access private members here
// exampleClassA.StudentID = 1000;
Commented it because we can not access private members here
// exampleClassA.Name = "Bill Formhals";
}
}
In the above example I have created a another public class and named it ChildClassB and inside this class I have declared a constructor and inside the constructor I have created an object of private class ExampleClassA and trying to assigns the values to class members , we are not able to access the private class here and when are trying to access the private members it will throw a compile time error as you can shown in below image.
When you are trying to explicitly declared class as private then it will throw an compile time error so do not use private keyword for class name bydefault classes are private if they dont have any modifiers into it.
Protected Modifiers are used with Protected Keyword . There is strict limitations for Protected modifiers in application to access it . We can access Protected members only in same class where it is declared and the class which derives this class. In simple words we can say that we can not access Protected decalred code anywhere in assembly or outside the assembly referenced in any other projects. Below is the screen images for Protected declared Classes with examples.
public class ExampleClassA
{
protected int StudentID { get; set; }
private string Name { get; set; }=string.Empty;
public int Salary { get; set; }
protected string Address { get; set; }
public ExampleClassA()
{
StudentID = 1200;
Name = "Cathy";
Address = "New York";
}
protected void PrintStudentDeatils()
{
StudentID = 1000;
Name = "Rocky";
Salary = 52000;
Console.WriteLine("Student Details are :" +" Student ID is "+StudentID+ " Name is :- " +Name+" Salary is :- "+Salary);
}
}
In the above example I have created a Public class and named it ExampleClassA and inside this class I have declared five members , among them four is protected and one is public so we can access this protected class members only in same class and in derived class.
public class ChildClassB :ExampleClassA
{
public void PrintFullName()
{
int stId = StudentID;
string address = Address;
int salry=Salary;
Console.WriteLine("Potected member StudentID fetched in derived class " + stId);
Console.WriteLine("Potected member Address fetched in derived class " + address);
Console.WriteLine("Below is the output of Protected method called from child class object ");
PrintStudentDeatils();
}
}
In the above example I have created a another public class and named it ChildClassB and derived it from ExampleClassA so that we can access the protected members inside it using a void method PrintFullName().
//In Program.cs file write the below lines for creating a child class object
//Using Child class object we can access protected members
ChildClassB classB = new ChildClassB();
classB.PrintFullName();
internal Modifiers are used with internal Keyword . There is strict limitations for internal modifiers in application to access it . We can access internal members any where in code but in same assembly. In simple words we can say that we can not access internal decalred code outside the assembly referenced in any other projects we have ho use it in the same assembly where it is created. Below is the screen images for internal declared Classes with examples.
internal class InternalExample
{
public int id { get; set; }
internal int type { get; set; }
internal string name { get; set; }=string.Empty;
internal void PrintFullName()
{
Console.WriteLine("Full Name displayed here");
}
}
In the above example I have created a internal class and named it InternalExample inside ConstructorExamples assembly and inside this class I have declared four members , among them three is internal and one is public so we can access this internal class members only in same assembly anywhere but outside the assembly this is not accessible.
internal class ConsumerClass
{
public ConsumerClass()
{
//InternalExample obj1 = new InternalExample();
Employee obj2 = new Employee();
obj2.Department = "It";
obj2.PrintFullName();
}
}
In the above example I have created a another internal class and named it ConsumerClass into a different assembly named it DataOperations and tried to creating an object of InternalExample class which is an internal class and located in different assembly but we are not able to access it due to its protection level and when we tried to create a object of Employee class which is public and from different assembly then we are able to access it because it is a public class.
As you can see in above image we can not access the internal class outside the assembly but we are able to access the public class outside the assembly as like in below image but we are also not able to see the internal members into the object list because these is restricted to access except public members.
In the below image we have the project structure in Solution explorer. As you can see here we have two assemblies first one is ConstructorExamples and second is DataOperations . We have created our internal class into the ConstructorExamples assembly and tried to access it into the DataOperations assembly inside the ConsumerClass.
About the Author
Sudheer Singh Chouhan is a Software Engineer having Expertise in Development Design and Architecting the
applications , Project Management , Designing Large Scale Databases in SQL Server since last 17 Years.
Skill Sets :- Microsoft .NET technologies like ASP.Net Core, Web API, LINQ, Web Forms, WinForms, SQL Server,
EntityFramework, Design Patterns, Solid Principles, Microservices, AWS Cloud.