#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;

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: