It is generally desirable to designate the member
variables of a class as private
. This
means that only member methods of that class can access their value.
When you prevent methods outside the class from directly accessing
member variables, you’re enforcing data
hiding
, which is part of the encapsulation of a class.
Object-oriented programmers are told that member variables should be private. That’s fine, but how do you provide access to this data to your clients? The answer for C# programmers is 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 solution is ideal. The client wants direct access to the state of the object. The class designer, however, wants to hide the internal state of the class in class fields and provide indirect access through a method. The property provides both the illusion of direct access for the client, and the reality of indirect access for the class developer.
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,
changing how that value is resolved would break the client. By
decoupling and forcing the client to go through a property, the Time
class can change how it manages its
internal state without breaking client code.
In short, properties provide the data hiding required by good
object-oriented design. Example
8-2 creates a property called Hour
, which is then discussed in the
paragraphs that follow.
Example 8-2. Properties
using System; namespace Properties { public class Time { // private member variables private int year; private int month; private int date; private int hour; private int minute; private int second; // create a property public int Hour { get { return hour; } set { hour = value; } } // public accessor methods public void DisplayCurrentTime( ) { System.Console.WriteLine( "Time: {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; } } class Tester { public void Run( ) { System.DateTime currentTime = System.DateTime.Now; Time t = new Time( currentTime ); t.DisplayCurrentTime( ); // access the hour to a local variable int theHour = t.Hour; // display it System.Console.WriteLine( "Retrieved the hour: {0}", theHour ); // increment it theHour++; // reassign the incremented value back through // the property t.Hour = theHour; // display the property System.Console.WriteLine( "Updated the hour: {0}", t.Hour ); } [STAThread] static void Main( ) { Tester t = new Tester( ); t.Run( ); } } }
The output should look something like this:
Time : 7/10/2008 12:7:43 Retrieved the hour: 12 Updated the hour: 13
You create a property by writing the property type and name
followed by a pair of braces. Within the braces, you can declare the
get
and set
accessors. These accessors are very
similar to methods, but they are actually part of the property itself.
The purpose of these accessors is to provide the client with simple ways
to retrieve and change the value of the private member hour
, as you’ll see.
Neither of these accessors has explicit parameters , though the set
accessor has an implicit parameter called value
, which is used to set the value of the
member variable.
In Example 8-2, the
declaration of the Hour
property
creates both get
and set
accessors:
public int Hour { get { return hour; } set { hour = value; } }
Each accessor has an accessor-body, which
does the work of retrieving or setting the property value. The property
value might be stored in a database (in which case, the accessor would
do whatever work is needed to interact with the database), or it might
just be stored in a private member variable (in this case, hour
):
private int hour;
The body of the get
accessor is similar to a class method that returns an object of the
type of the property. In Example 8-2, the accessor for
the Hour
property is similar to a
method that returns an int
. It
returns the value of the private member variable hour
in which the value of the property has
been stored:
get { return hour; }
In this example, the value of a private int
member variable is returned, but you
could just as easily retrieve an integer value from a database or
compute it on the fly.
Remember, this description is from the perspective of the
author of the Time
class. To the
client (user) of the Time
class,
Hour
is a property, and how the
Time
class returns its hour is
encapsulated within the Time
class—the client doesn’t know or care.
Whenever you need to retrieve the value (other than to assign to
it), the get
accessor is invoked.
For example, in the following code, the value of the Time
object’s Hour
property is assigned to a local
variable.
To the client, the local variable theHour
is assigned the value of the
Hour
property of t
(the Time
object). To the creator of the Time
object, however, the get
accessor is called, which, in this case,
returns the value of the hour
member variable:
Time t = new Time(currentTime);int theHour = t.Hour;
The set
accessor sets
the value of a property. When you define a set
accessor, you must use the value
keyword to represent the argument
whose value is assigned to 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 first line increments a local variable named theHour
. As far as the client is concerned,
that new value is assigned to the Hour
property of the local time object
t
. To the author of the Time
class, however, the local variable
theHour
is passed in to the set
accessor as the implicit parameter value
and assigned (in this case) to the
local member variable hour
.
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.