Properties in a class protect the fields in a class. These properties by using the get and set methods
it helps the fields in reading and writing.
It basically encapsulates the fields. The private fields are exposed through properties.
The property contains a variable and methods. These methods get and set.
Example:
There are 3 types of Properties in C#. These are:
- Read Only:
Read Only properties will have only the get method. To make the StrAddress a read-only property, in the above example remove the set method.
- Write Only:
Write only properties will have only the set method. To make the StrAddress a write only property, in the above example remove the get method.
- Both:
Here, we will have both get and set methods.
Example:
The output will be: