Introduction
In this article, we will learn about the Properties of C#. Properties enable us to manipulate the private data fields of a class. Properties are those class members that provide a flexible way to get and set data of private fields outside that class.
The most crucial point that we need to remember is that property in C# is never used to store data. It acts as an interface for transferring the data.
Now, we will look at different types of accessors that manipulate the private data fields of a class.
Recommended Topic, Palindrome in C# and Ienumerable vs Iqueryable.
Type of Accessors
There are two types of accessors:
- set accessor: It is a method that is used to set data of private data field present in a class.
- get accessor: It is a method that is used to get data of private data fields present in a class.
We will see an example to understand how to get and set the value of private data members.
Example
|
Output
![](https://files.codingninjas.in/article_images/properties-of-c-0-1649600408.webp)
In the above example, we declare the data fields of the Employee class as private. So, these data fields will not be accessible directly outside the Employee class. So, in the Program class, we transferred the data into the data field with the help of properties. As we provide the abstraction to the data fields, this is known as data abstraction. So properties will provide data abstraction.