Encapsulating Data with Properties

Properties allow clients to access class state as if 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 does not 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-11.

Example 4-11. Using a property

public class Time
{
   // public accessor methods
   public void DisplayCurrentTime(  )
   {

      System.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;
         }
     }

    // private member variables
   private int year;
   private int month;
   private int date;
   private int hour;
   private int minute;
   private int second;
}

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

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

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() method has an implicit parameter value as shown next.

In Example 4-11, 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 which does the work of retrieving and 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 reference the property (other than to assign to it), the get accessor is invoked to read the value of the property:

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 advantage of this approach is that the client can interact with the properties directly, without sacrificing the data hiding and encapsulation sacrosanct in good object-oriented design.

Get Programming C# 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.