Encapsulation
Encapsulation is essentially just a method of data organization. It involves organizing data and methods in the forms of classes so the whole program is essentially just based around classes and the data and methods are encapsulated within those classes. This encapsulation provides a way to control access to the internal state of an object, promoting modularity, maintainability, and security in a software system.
In Encapsulation, you use the Access Modifiers like public
, private
and protected
to hide all most of the fields and methods of a class and expose only the ones that are needed to be used from outside.
Since most of the data is directly inaccessible outside the class, you use getters and setters to access or modify the data.
One good example is a Customer
class which defines the customer of a bank:

index.cs
1234567891011121314151617181920212223242526272829303132public class Customer { private double accountBalance; private string customerName; private int pinCode; public Customer(double accountBalance, string customerName, int pinCode) { this.accountBalance = accountBalance; this.customerName = customerName; this.pinCode = pinCode; } public double getBalance(int pinCode) { if (pinCode == this.pinCode) return this.accountBalance; return } public void Deposit(double amount, int pinCode) { if(pinCode == this.pinCode) accountBalance += amount; } public void Withdraw(double amount, int pinCode) { if(pinCode == this.pinCode) accountBalance -= amount; } }
In the above example, no field is directly accessible or modifiable from outside. Instead you use methods like Deposit
and Withdraw
to modify the value whenever needed. Similarly to access the value of the balance you use the getBalance
method.
The public
keyword is generally discouraged to be used unless necessary.
To remind, in case we want to keep a field inaccessible from outside the class but accessible from the derived classes, we can use the protected
access modifier.
1. What role do access modifiers play in encapsulation in C#?
2. Which of the following access specifiers should be used the least in order to preserve encapsulation?
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you provide an example of encapsulation in code?
Why is using public fields discouraged in encapsulation?
How do getters and setters improve security?
Awesome!
Completion rate improved to 2.04
Encapsulation
Swipe to show menu
Encapsulation is essentially just a method of data organization. It involves organizing data and methods in the forms of classes so the whole program is essentially just based around classes and the data and methods are encapsulated within those classes. This encapsulation provides a way to control access to the internal state of an object, promoting modularity, maintainability, and security in a software system.
In Encapsulation, you use the Access Modifiers like public
, private
and protected
to hide all most of the fields and methods of a class and expose only the ones that are needed to be used from outside.
Since most of the data is directly inaccessible outside the class, you use getters and setters to access or modify the data.
One good example is a Customer
class which defines the customer of a bank:

index.cs
1234567891011121314151617181920212223242526272829303132public class Customer { private double accountBalance; private string customerName; private int pinCode; public Customer(double accountBalance, string customerName, int pinCode) { this.accountBalance = accountBalance; this.customerName = customerName; this.pinCode = pinCode; } public double getBalance(int pinCode) { if (pinCode == this.pinCode) return this.accountBalance; return } public void Deposit(double amount, int pinCode) { if(pinCode == this.pinCode) accountBalance += amount; } public void Withdraw(double amount, int pinCode) { if(pinCode == this.pinCode) accountBalance -= amount; } }
In the above example, no field is directly accessible or modifiable from outside. Instead you use methods like Deposit
and Withdraw
to modify the value whenever needed. Similarly to access the value of the balance you use the getBalance
method.
The public
keyword is generally discouraged to be used unless necessary.
To remind, in case we want to keep a field inaccessible from outside the class but accessible from the derived classes, we can use the protected
access modifier.
1. What role do access modifiers play in encapsulation in C#?
2. Which of the following access specifiers should be used the least in order to preserve encapsulation?
Thanks for your feedback!