The purpose of attributes in C# is to encapsulate fields and are for the sake of security of program data. This article analyzes the read-only and write-only attributes in C# in an example form.
For read-only or write-only attribute definitions:
1. You can only read or write without writing one of the get\set methods.
for example:
private int a; public int A{ get { return a; } }
2. Use private to protect, outside the class also means read-only or write-only
for example:
private int a; public int A{ private get { return a; } set { a = value; } }
It should be noted here that the properties defined in this way are in C# 3.0 and later. When no other logic is needed in the accessor of the properties, the automatically implemented properties can make the property declaration more concise.
The compiler will create a private anonymous support field that can only be accessed through the property's get and set accessors.
public int A{get;set;}
Remember! This way, one of them (get/set) cannot be omitted for read-only or write-only.
But use private protection:
public int A{get;private set;}