In C#, access modifiers are keywords used to define the visibility or accessibility of types and their members (e.g., classes, methods, properties, fields). These modifiers specify where a particular class member or type can be accessed from, controlling encapsulation and ensuring proper access levels based on your design needs.
OR
Access modifiers in C# are keywords that control the visibility or accessibility of classes, methods, properties, and other members. They determine from where these members can be accessed.
There are five main access modifiers in C#:
- public
- private
- protected
- internal
- protected internal
public Access Modifier
A public member or class is accessible from anywhere, inside or outside the class, and even from different assemblies.
using System;
class Employee {
public string name;
public void getEmployeeName() {
Console.WriteLine("Name: " + name);
}
}
class Program
{
static void Main()
{
// Creating objects (instances of the Employee class)
Employee emp1 = new Employee();
emp1.name = "John";
emp1.getEmployeeName();
}
}
Explanation:
- The name field is declared public, meaning they can be accessed directly from outside the class (in the Main method).
- The method getEmployeeName is a public, so it can be called from any other code, not just within the Employee class.
private Access Modifier
A private member is accessible only within the same class. It cannot be accessed from outside the class.
Example:
using System;
public class Employee {
private string name;
public Employee(string empName) {
name = empName;
}
public void getEmployeeName() {
Console.WriteLine("Name: " + name);
}
}
class Program
{
static void Main()
{
// Creating objects (instances of the Employee class)
Employee emp1 = new Employee("John");
//Console.WriteLine("Name: " + name); Private member can't access outside the class
emp1.getEmployeeName();
}
}
Explanation:
1. The name field is private, meaning it cannot be accessed directly from outside the class.
2. It can only be accessed within the class itself (for example, in the getEmployeeName method).
protected Access Modifier
A protected member is accessible within the containing class and by derived classes.
using System;
public class Employee {
public string name;
protected string designation;
public Employee(string empName) {
this.name = empName;
this.designation = "Manager"; // Default designation
}
}
public class Manager : Employee {
public Manager(string name): base(name) {
// Explicitly calling the Employee constructor
}
public void displayDesignation() {
Console.WriteLine("Designation: " + designation); // Accessing the protected field
}
}
class Program
{
static void Main()
{
// Creating a Manager object and displaying the name and designation
Manager mg = new Manager("Tom");
Console.WriteLine("Name: " + mg.name);
mg.displayDesignation(); // Output: Designation: Manager
}
}
Explanation:
1. The designation field is protected, meaning it can be accessed by the Manager class, which derives from the Employee class.
2. It cannot be accessed from outside the Employee or Manager classes (e.g., in the Main method).
internal Access Modifier
An internal member is accessible only within the same assembly (i.e., within the same project or code file). It is not accessible from other assemblies or projects.
Example:
using System;
internal class Employee
{
internal string name; // Accessible within this assembly
public Employee(string empName)
{
this.name = empName;
}
public void DisplayName()
{
Console.WriteLine($"Name: {this.name}");
}
}
class Program
{
static void Main()
{
Employee emp = new Employee("John");
emp.DisplayName(); // Can access internal members within the same assembly
}
}
Output:
Explanation:
1. The Employee class and its name field are marked as internal. They can only be accessed within the same assembly (project).
2. If you tried to access this class from a different assembly, you would get an error.
protected internal Access Modifier
A protected internal member is accessible within the same assembly and from derived classes in other assemblies.
Example:
using System;
public class Employee
{
protected internal string name; // Accessible within the same assembly and by derived classes
public Employee(string empName)
{
this.name = empName;
}
}
public class Manager : Employee
{
public Manager(string empName) : base(empName) {}
public void displayName()
{
Console.WriteLine($"Name: {this.name}"); // Accessible in derived class
}
}
class Program
{
static void Main()
{
Manager manager = new Manager("John");
manager.displayName(); // Accessible in derived class
Employee emp = new Employee("Tom");
Console.WriteLine("Emp name is "+emp.name); // Accessible within the same assembly
}
}
Output:
Emp name is Tom
Explanation:
1. The name field is protected internal, so it can be accessed both within the same assembly and by derived classes (e.g., Manager).
2. It cannot be accessed from code outside the assembly unless it’s part of a derived class.