-
Notifications
You must be signed in to change notification settings - Fork 5
Encapsulation
Sann Lynn Htun edited this page Nov 19, 2024
·
1 revision
Encapsulation is one of the core principles of object-oriented programming (OOP) in C#. It is the process of bundling data (fields) and methods (functions) that operate on the data into a single unit, usually a class. Encapsulation restricts direct access to certain components of an object, enhancing security, modularity, and code maintainability.
-
Data Hiding
- Fields or properties are kept private, limiting direct access.
- Access is provided through public methods or properties (getters and setters).
-
Access Modifiers
- Control the visibility of class members.
- Examples:
private
,protected
,public
,internal
.
-
Properties
- Provide a controlled way to access or modify private fields.
-
Improved Modularity
- Classes act as self-contained units, making the code easier to maintain.
-
Data Protection
- Prevents unauthorized access and modification.
-
Ease of Maintenance
- Internal implementation changes do not affect external code.
-
Improved Readability
- Clearly defined interfaces for accessing data.
class Person {
// Private field
private string name;
// Public property
public string Name {
get { return name; } // Getter
set {
if (!string.IsNullOrEmpty(value)) {
name = value;
} else {
throw new ArgumentException("Name cannot be empty.");
}
} // Setter
}
// Public method
public void DisplayInfo() {
Console.WriteLine($"Name: {name}");
}
}
// Usage
Person person = new Person();
person.Name = "John Doe"; // Using the property
person.DisplayInfo(); // Output: Name: John Doe
class BankAccount {
private double balance;
// Read-only property
public double Balance {
get { return balance; }
}
// Write-only property
public double Deposit {
set {
if (value > 0) {
balance += value;
}
}
}
}
// Usage
BankAccount account = new BankAccount();
account.Deposit = 500; // Adding money to the account
Console.WriteLine(account.Balance); // Output: 500
class Product {
private string name;
private double price;
// Constructor for initialization
public Product(string name, double price) {
this.name = name;
this.price = price;
}
// Public method
public void Display() {
Console.WriteLine($"Product: {name}, Price: {price:C}");
}
}
// Usage
Product product = new Product("Laptop", 999.99);
product.Display(); // Output: Product: Laptop, Price: $999.99
Modifier | Description | Example Usage |
---|---|---|
private |
Accessible only within the class. | Internal data fields. |
public |
Accessible from anywhere. | Methods, interfaces, properties. |
protected |
Accessible within the class and derived classes. | For inheritance scenarios. |
internal |
Accessible within the same assembly. | Shared classes within a project. |
protected internal |
Accessible within the same assembly or derived classes. | Mixed accessibility requirements. |
- Encapsulation uses access modifiers to control visibility and access.
- Properties are a structured way to enforce rules when accessing fields.
- Private fields provide internal protection while exposing controlled access via public interfaces.
- Encapsulation ensures data integrity by restricting direct access to class members.
- Controlled access is provided via properties and methods.
- Benefits include better data protection, modular code, and easier maintenance.
- Use encapsulation to safeguard your codebase, ensuring robustness and clarity in object design.
Encapsulation is a vital component of object-oriented programming, promoting cleaner, safer, and more maintainable code.
-
Introduction to C#
What is C#? -
Variables and Data Types
Understand how to declare and use variables. -
Operators
Arithmetic, relational, and logical operators in C#. -
Control Statements
If-else, switch, and looping constructs.
-
Classes and Objects
Basics of OOP in C#. -
Inheritance
How to use inheritance effectively. -
Polymorphism
Method overriding and overloading. -
Encapsulation
Understanding access modifiers.
-
LINQ Basics
Working with Language Integrated Query. -
Asynchronous Programming
Introduction to async/await. -
File Handling
Reading and writing files.
-
Number Formatting
Formatting numbers in C#. -
Exception Handling
Handling runtime errors effectively. -
Working with Dates
DateTime and time zone handling. -
Using Keyword in C#
Different usages of theusing
keyword in C#.
-
Setting Up Development Environment
Installing Visual Studio and .NET SDK. - Useful Libraries Libraries and tools for C# development.