#244 – Defining a Get Accessor for a Property

A get accessor for a property is the code that executes when the client of the object tries to read the property’s value.

The get accessor is defined using the get keyword.  The get accessor should return a value of the same type as the property itself.  You often just return the value of an internal private variable, which is known as the backing field.

In the example below, we define a get accessor for a property and return the value of the internal backing field whenever the property is read.

    // Backing field--where the property value is actually stored
    private string name;

    public string Name
        // Get accessor allows the property to be read
        get { return name; }

Below is an example of the Name property being read.

    // Read the Name property
    string objectName = myObject.Name;