#243 – Property Get and Set Accessors

You can define a property in a class as a way of encapsulating a private field.  When declaring a property in a class, you define the property’s type, its name, and code for the property’s accessors.  The accessors are the means by which a user of the class reads and writes the property’s value.

When defining the accessors, you can have one of the following combinations:

  • Just a get accessor   (property is read-only)
  • Just a set accessor  (property is write-only)
  • Both get/set accessors  (property is read/write)

The get accessor defines the code that executes when some code tries to read the property’s value.

The set accessor defines the code that executes when a property value is written to.

Advertisement