| ||||||
| ||||||

PropertiesGoals:
OverviewProperties model the traits of an object or a class. C# provides special syntax to define and access properties. Programmers are encouraged to use the special syntax to implement properties because it results in clean client code and makes the intent of the class designer explicit to both human readers and automated tools. MotivationA class is typically a software model of a real world thing. The thing being modeled has certain properties; for example, a person has a name and an age, a stock has a symbol and a price, a circle has a radius, and so on. Other common terms for the same concept are trait, characteristic, or quality. A property can be implemented simply by defining a field to store the current value as shown below.
Client code often needs access to the property in order to read and/or write the value. The simplest way to achieve this is to make the fields public. Public data makes the resulting client code clean and simple. A write operation is accomplished by using the assignment operator to load a new value into the field and a read operation is a normal field access.
Despite its simplicity, public data is not used very often in practice since it
has some disadvantages from a design point of view. One flaw is that there is no
way for the class to perform error checking on the data being loaded into the
fields. For example, a client could give a person a negative age or an empty
name and the class would not be able to detect the error. Another problem is
that there is no way to implement a read-only property since public data is
implicitly readable and writeable by all external code. The common way to avoid
the problems associated with public data is to make the data private and provide
public read and write access methods. By convention, the access methods are typically
named
The client code for properties implemented with access methods is not quite as
clean as the public data case. The write operation for a public field uses an
expression such as
C# propertiesThe two goals of access control and clean client code are somewhat in conflict. Public data yields simple and intuitive client code but does not give the class designer enough control. Private data / public access methods provide the ability to do error checking and implement read-only properties but leads to less readable client code. To solve this dilemma, C# offers special syntax to implement properties that combines the advantages of both conventional techniques. C# provides special language support for the implementation of properties which specifies a structured way to define the read and write accessors. There are five components of the property declaration: access level, type, name, write accessor, and read accessor. The basic structure is shown in the figure below.
Recall that a property definition typically has three components: an instance
variable for data storage, a write access method, and a read access method.
A C# property definition provides only the accessors and does not include any
needed data storage. The most common case then, is to define a field to store
the property data. The common wisdom suggests choosing a name for the field
that is similar to the name of the property; for example, a variable called
The implementation of the accessors is perhaps the most interesting part of the
syntax. The write accessor for a property is named
The read access method for a property is named
The client code for C# properties is identical to accessing a public field.
The compiler analyzes how the property is being used and invokes the appropriate
accessor automatically. Specifically, the
The C# property syntax gives much of the power of the private data / public access
methods pattern while retaining all the benefits of a public data implementation.
In particular, a property can be read-only, write-only, or read-write. This is
accomplished simply by providing or omitting the Static propertyA property that applies to an entire class rather than to an instance can be created by applying the keyword static to the property definition as shown below. As with a static method, inside the accessors of a static property the instance methods and instance fields of the class are not accessible. That is, only the static parts of the class are usable and any attempt to access non-static members is a compile time error.
A static property must be accessed through the type name. It is a compile time error to attempt access using an instance of the class.
Benefits
The decision to support properties as a first class language feature is an interesting one.
Adding the feature made the language larger and thus more difficult to learn; however,
there are several good arguments as to why the special property syntax is worthwhile.
First, the client code using the property syntax is much cleaner than using traditional
access methods. Second, it makes the code more self documenting since otherwise,
maintenance programmers would need to deduce the existence of a property from the presence
of a pair of set/get methods. Finally, the property syntax allows property information
to be carried through compilation and into the resulting IL code. The IL can be interrogated
programmatically using the This material is excerpted from the Programming C# course offered by DevelopMentor. |