- It is a class method that encapsulates a getter and setter for accessing the class fields
- When can define just a getter or just a setter if required
- A property without a getter is a read-only and one without a setter is write-only
- The setter is passed an
implicit field
calledvalue
which holds the value passed by the user that needs to be stored in the field
Auto-Properties
- When we just want to set and get the value of fields (not perform any validation) we can use auto-implemented properties to write even lesser code
- The field declared using auto-implemented properties is always private (Internally a private backing field is created for holding the values)
- When using auto-implemented properties both the set and the get method have to be defined (The Setter can be made private to prevent usage)
- Auto-implemented properties should be placed at the top of the class followed by the Constructor and then Properties (Convention)
In place of private set;
the init;
keyword can also be used to define a Property that can only be set at initialization and then only read