Curriculum
Access Modifiers in C# are keywords used to control the visibility and accessibility of classes, methods, properties, fields, constructors, and other members within a program. Access Modifiers in C# play a critical role in Object-Oriented Programming by enforcing Encapsulation, improving security, protecting sensitive data, and maintaining application integrity. Every professional .NET developer uses Access Modifiers in C# while building ASP.NET Core Applications, MVC Projects, Web APIs, Desktop Applications, Cloud Solutions, and Enterprise Software Systems.
Understanding Access Modifiers in C# is essential because they determine who can access specific parts of your application and help create secure, maintainable software.
Access Modifiers in C# define the accessibility level of classes and class members.
They answer questions such as:
Without Access Modifiers, applications would expose sensitive data and become difficult to maintain.
Access Modifiers in C# help developers:
Access control is one of the foundations of enterprise software development.
C# provides the following Access Modifiers:
| Access Modifier | Accessibility |
|---|---|
| public | Accessible from anywhere |
| private | Accessible only within the same class |
| protected | Accessible within the class and derived classes |
| internal | Accessible within the same project (assembly) |
| protected internal | Accessible within assembly and derived classes |
| private protected | Accessible within the same assembly and derived classes only |
Each modifier serves a different purpose.
The public Access Modifier allows access from anywhere.
Example:
public class Student
{
public string Name;
}
Usage:
Student student =
new Student();
student.Name = "Rahul";
Output:
Rahul
Public members are visible throughout the application.
Use public when:
Examples:
Login()
Register()
ProcessPayment()
Public members form the external interface of a class.
The private Access Modifier restricts access to the same class only.
Example:
public class Student
{
private int age;
}
Usage:
Student student =
new Student();
student.age = 20;
Output:
Compilation Error
The field cannot be accessed directly outside the class.
Private members help:
Private fields are widely used in professional software development.
Example:
public class Student
{
private int age;
public int Age
{
get
{
return age;
}
set
{
if(value > 0)
{
age = value;
}
}
}
}
Usage:
Student student =
new Student();
student.Age = 20;
Output:
20
This is a common Encapsulation pattern.
The protected Access Modifier allows access within:
Example:
public class Person
{
protected string Name;
}
Derived Class:
public class Student : Person
{
public void SetName()
{
Name = "Rahul";
}
}
Output:
Rahul
Protected members support inheritance.
Parent Class:
public class Employee
{
protected double Salary;
}
Child Class:
public class Manager :
Employee
{
public void SetSalary()
{
Salary = 50000;
}
}
The derived class can access protected members directly.
The internal Access Modifier allows access within the same assembly or project.
Example:
internal class Employee
{
}
Usage:
Employee employee =
new Employee();
Output:
Accessible Inside Project
The class cannot be accessed from another assembly.
Use internal when:
This improves software modularity.
The protected internal Access Modifier combines:
Protected
+
Internal
Accessibility:
Example:
protected internal string Department;
This modifier is useful in large enterprise systems.
The private protected Access Modifier allows access:
Example:
private protected int EmployeeId;
This provides more restrictive inheritance access.
| Modifier | Same Class | Derived Class | Same Assembly | Outside Assembly |
|---|---|---|---|---|
| public | Yes | Yes | Yes | Yes |
| private | Yes | No | No | No |
| protected | Yes | Yes | No | No |
| internal | Yes | Yes | Yes | No |
| protected internal | Yes | Yes | Yes | Yes (Derived) |
| private protected | Yes | Yes | Yes | No |
This table is commonly used in interviews.
Example:
public class BankAccount
{
private double balance;
public double Balance
{
get
{
return balance;
}
}
public void Deposit(
double amount)
{
balance += amount;
}
}
Usage:
BankAccount account =
new BankAccount();
account.Deposit(1000);
Output:
1000
The balance cannot be modified directly.
This demonstrates proper Encapsulation.
Example:
public class User
{
public void Login()
{
}
private void Validate()
{
}
}
Public Method:
Accessible Everywhere
Private Method:
Accessible Only Inside Class
Method access control improves application security.
Example:
public class Student
{
private Student()
{
}
}
Usage:
Student student =
new Student();
Output:
Compilation Error
Private constructors are commonly used in Singleton Design Patterns.
Protected Data:
Account Balance
PIN
Transactions
Protected Data:
Payment Information
Order Details
Customer Data
Protected Data:
Medical Records
Prescriptions
Patient History
Protected Data:
Exam Results
Attendance
Student Records
Access Modifiers help secure sensitive information.
Avoid exposing all members publicly.
Use private fields whenever possible.
Choose the least permissive modifier.
Protect confidential information carefully.
Access Modifiers in C# are heavily used in:
A strong understanding of Access Modifiers in C# is essential for creating secure and maintainable .NET applications.
Access Modifiers are keywords that control the accessibility of classes and class members.
Public members are accessible everywhere, while private members are accessible only within the same class.
Protected members are accessible within the class and derived classes.
Internal members are accessible only within the same assembly or project.
Private fields protect data and support Encapsulation.
They improve security, maintainability, Encapsulation, and software architecture.
WhatsApp us