Curriculum
Encapsulation in C# is one of the four fundamental pillars of Object-Oriented Programming (OOP). Encapsulation in C# is the process of wrapping data and methods into a single unit while restricting direct access to sensitive information. Encapsulation in C# helps developers protect data, improve security, increase maintainability, and build robust software applications. Every professional .NET developer uses Encapsulation in C# while developing ASP.NET Core Applications, MVC Projects, Web APIs, Desktop Applications, Mobile Applications, Cloud Solutions, and Enterprise Software Systems.
Understanding Encapsulation in C# is essential because it provides controlled access to object data and prevents unauthorized modifications.
Encapsulation in C# is the process of hiding internal object details and exposing only the necessary functionality to external users.
Encapsulation combines:
into a single unit called a class.
It also controls access to data using:
This helps maintain data integrity and security.
Encapsulation in C# helps developers:
Most modern applications rely heavily on Encapsulation.
Consider an ATM Machine.
Users can:
Users cannot directly access:
This is a real-world example of Encapsulation.
Only necessary functionality is exposed while internal details remain hidden.
Example:
public class Student
{
public int Age;
}
Usage:
Student student =
new Student();
student.Age = -10;
This creates invalid data.
Output:
Age = -10
A student’s age should never be negative.
Without Encapsulation, data can become inconsistent.
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
Invalid values can now be prevented.
This is the primary goal of Encapsulation in C#.
Encapsulation relies heavily on Access Modifiers.
Accessible only within the same class.
Example:
private int age;
Accessible from anywhere.
Example:
public string Name;
Accessible within the class and derived classes.
Example:
protected int Marks;
Accessible within the same project.
Example:
internal string Department;
Access Modifiers help enforce Encapsulation.
Private Fields store data securely.
Example:
public class Employee
{
private double salary;
}
External classes cannot directly access:
salary
This protects sensitive information.
Properties provide controlled access to private fields.
Example:
public class Employee
{
private double salary;
public double Salary
{
get
{
return salary;
}
set
{
salary = value;
}
}
}
Usage:
Employee employee =
new Employee();
employee.Salary = 50000;
Properties are widely used in modern .NET development.
The get accessor retrieves data.
Example:
get
{
return salary;
}
Usage:
Console.WriteLine(
employee.Salary);
Output:
50000
The get accessor allows controlled reading of data.
The set accessor assigns data.
Example:
set
{
salary = value;
}
Usage:
employee.Salary = 60000;
The set accessor allows controlled updates.
Modern C# simplifies property creation.
Example:
public string Name
{
get;
set;
}
Equivalent to:
private string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
Auto-properties improve readability.
Example:
public string CompanyName
{
get;
}
=
"Groot Software";
Output:
Groot Software
Users can read but cannot modify the value.
Example:
private string password;
public string Password
{
set
{
password = value;
}
}
The value can be assigned but not retrieved.
This is useful for sensitive information.
Example:
public class Student
{
private int marks;
public int Marks
{
get
{
return marks;
}
set
{
if(value >= 0 &&
value <= 100)
{
marks = value;
}
}
}
}
Usage:
student.Marks = 90;
Output:
90
Validation prevents invalid data entry.
Example:
public class BankAccount
{
private double balance;
public double Balance
{
get
{
return balance;
}
}
public void Deposit(
double amount)
{
if(amount > 0)
{
balance += amount;
}
}
}
Usage:
BankAccount account =
new BankAccount();
account.Deposit(1000);
Console.WriteLine(
account.Balance);
Output:
1000
The balance cannot be modified directly.
This demonstrates proper Encapsulation.
| Encapsulation | Data Hiding |
|---|---|
| Combines data and methods | Restricts access |
| Uses classes | Uses access modifiers |
| Improves structure | Improves security |
| Supports OOP | Supports Encapsulation |
Data Hiding is a component of Encapsulation.
Protects sensitive data.
Changes can be made internally without affecting external code.
Validation rules can be updated easily.
Encapsulated classes can be reused across applications.
Supports enterprise-level software development.
These advantages make Encapsulation one of the most important OOP principles.
Account Balance
Transactions
Customer Details
Protected through encapsulated classes.
Product Price
Order Information
Payment Data
Managed securely using properties.
Patient Records
Medical History
Appointments
Protected using Encapsulation.
Student Information
Attendance
Exam Results
Managed securely using controlled access.
Encapsulation is used in almost every enterprise software application.
Avoid:
public int Age;
Prefer:
private int age;
with properties.
Always validate important data.
Keep confidential information private.
Choose appropriate access levels.
Encapsulation in C# is heavily used in:
A strong understanding of Encapsulation in C# is essential for becoming a professional .NET developer.
Encapsulation is the process of combining data and methods into a single class while restricting direct access to internal data.
It improves security, maintainability, flexibility, and data integrity.
Private fields store data that should not be directly accessible from outside the class.
Properties provide controlled access to private fields using get and set accessors.
Data validation ensures only valid values are assigned to object properties.
It helps developers build secure, maintainable, and scalable enterprise applications.
WhatsApp us