C# Encapsulation

HTML
CSS
C#
SQL

Encapsulation

The meaning of Encapsulation is to make sure that “sensitive” data is hidden from users. To achieve this, you must:
– declare fields/variables as private
– provide public get and set methods, through properties, to access and update the value of a private field
– Properties
We have learned from the previous chapter that private variables can only be accessed within the same class (an outside class has no access to it). However, sometimes we need to access them – and it can be done with properties.

Properties
– Property is a class member that provides a secure way for a class to expose its private fields.
– Properties can be used as if they are public data members, but they are actually special methods called accessors
– Properties enable a class to expose a public way of getting and setting values, while hiding implementation or verification code.
– The value keyword is used to define the value being assigned.
Syntax:
<access_modifier> <return_type> <property_name>
{
get { // body }
set { // body }
}