Encapsulating Data with Properties

Properties allow clients to access the state of the object as though they were accessing member fields directly, while actually implementing that access through a class method.

This is ideal. The client wants direct access to the state of the object and doesn't want to work with methods. The class designer, however, wants to hide the internal state of his class in class members, and provide indirect access through a method.

By decoupling the class state from the method that accesses that state, the designer is free to change the internal state of the object as needed. When the Time class is first created, the Hour value might be stored as a member variable. When the class is redesigned, the Hour value might be computed or retrieved from a database. If the client had direct access to the original Hour member variable, the change to computing the value would break the client. By decoupling and forcing the client to go through a method (or property), the Time class can change how it manages its internal state without breaking client code.

Properties meet both goals: they provide a simple interface to the client, appearing to be a member variable. They are implemented as methods, however, providing the data-hiding required by good object-oriented design, as illustrated in Example 4-12.

Example 4-12. Using a property

using System;

namespace UsingAProperty
{
  public class Time
  {
    // private member variables
    private int year;
    private int month;
    private int date;
    private int hour;
    private int minute;
    private int second;

    // public accessor methods
    public void DisplayCurrentTime(  )
    {

      Console.WriteLine(
        "Time\t: {0}/{1}/{2} {3}:{4}:{5}",
        month, date, year, hour, minute, second );
    }

    // constructors
    public Time( System.DateTime dt )
    {
      year = dt.Year;
      month = dt.Month;
      date = dt.Day;
      hour = dt.Hour;
      minute = dt.Minute;
      second = dt.Second;
    }

    // create a property

    public int Hour
    {
      get
      {
        return hour;
      }

      set
      {
        hour = value;
      }
    }
  }

  public class Tester
  {
    static void Main(  )
    {
      DateTime currentTime = DateTime.Now;
      Time t = new Time( currentTime );
      t.DisplayCurrentTime(  );

      int theHour = t.Hour;
      Console.WriteLine( "\nRetrieved the hour: {0}\n", theHour );
      theHour++;
      t.Hour = theHour;
      Console.WriteLine( "Updated the hour: {0}\n", theHour );
    }
  }
}

Output:

Time    : 9/20/2007 17:16:42

Retrieved the hour: 17

Updated the hour: 18

To declare a property, write the property type and name followed by a pair of braces. Within the braces, you may declare get and set accessors. Neither of these has explicit parameters, though the set( ) accessor has an implicit parameter value, as shown next.

In Example 4-12, Hour is a property. Its declaration creates two accessors: get and set.

public int Hour
{
  get
  {
    return hour;
  }

  set
  {
    hour = value;
  }
}

Each accessor has an accessor body that does the work of retrieving or setting the property value. The property value might be stored in a database (in which case, the accessor body would do whatever work is needed to interact with the database), or it might just be stored in a private member variable:

private int hour;

The get Accessor

The body of the get accessor is similar to a class method that returns an object of the type of the property. In the example, the accessor for Hour is similar to a method that returns an int. It returns the value of the private member variable in which the value of the property has been stored:

get
{
  return hour;
}

In this example, a local int member variable is returned, but you could just as easily retrieve an integer value from a database, or compute it on the fly.

Whenever you read the property, the get accessor is invoked:

Time t = new Time(currentTime);
int theHour = t.Hour;

In this example, the value of the Time object's Hour property is retrieved, invoking the get accessor to extract the property, which is then assigned to a local variable.

The set Accessor

The set accessor sets the value of a property and is similar to a method that returns void. When you define a set accessor, you must use the value keyword to represent the argument whose value is passed to and stored by the property:

set
{
  hour = value;
}

Here again, a private member variable is used to store the value of the property, but the set accessor could write to a database or update other member variables as needed.

When you assign a value to the property, the set accessor is automatically invoked, and the implicit parameter value is set to the value you assign:

theHour++;
t.Hour = theHour;

The two main advantages of this approach are that the client can interact with the properties directly, without sacrificing the data-hiding and encapsulation sacrosanct in good object-oriented design, and that the author of the property can ensure that the data provided is valid.

Property Access Modifiers

It is possible to set an access modifier (protected, internal, private) to modify access to either the get or set accessor. To do so, your property must have both a set and a get accessor, and you may modify only one or the other. Also, the modifier must be more restrictive than the accessibility level already on the property or the indexer (thus, you may add protected to the get or set accessor of a public property, but not to a private property):

public string MyString
{
  protected get { return myString; }
  set { myString = value; }
}

In this example, access to the get accessor is restricted to methods of this class and classes derived from this class, whereas the set accessor is publicly visible.

Tip

Note that you may not put an access modifier on an interface (see Chapter 8) or on explicit interface member implementation. In addition, if you are overriding a virtual property or index (as discussed next), the access modifier must match the base property's access modifier.

Get Programming C# 3.0, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.