by kirupa |
1 January 2007In the
previous page,
you learned the basics of how to use structs as well as some
important things to keep in mind about them. In this page, I
will expand on the earlier example by using Properties to
make the code more maintainable.
In my simple example, you access the public fields for your
struct instance directly. While that is an acceptable way to
store and retrieve data, they limit extensibility. When
writing programs, you want to try your best to add/change
functionality without breaking existing code.
When you access public fields directly, you are simply
retrieving a stored value. If you later decide to perform
some sort or processing instead of retrieving the raw data,
for example you want the age in days instead of years, such
a change may break parts of our application that depended
strictly on the earlier implementation. For a simple example
such as what you see here, re-writing some code is painless.
When you are working on a larger application, such bug fixes
can be time-consuming.
One solution to that problem is by bypassing public
fields and using Properties. Before I continue on, let me
provide you the code for our example using Properties:
- public
struct
Person
- {
- private
string
firstName;
- private
string
lastName;
- private
int
age;
-
- public
string
FirstName
- {
- get
- {
- return
firstName;
- }
- set
- {
- firstName
=
value;
- }
- }
- public
string
LastName
- {
- get
- {
- return
lastName;
- }
- set
- {
- lastName
=
value;
- }
- }
- public
int
Age
- {
- get
- {
- return
age;
- }
- set
- {
- age
=
value;
- }
- }
- }
- class
Program
- {
- static
void
Main(string[]
args)
- {
- Person
homer
=
new
Person();
- homer.FirstName
=
"Homer";
- homer.LastName
=
"Simpson";
- homer.Age
=
36;
-
- Console.WriteLine("Person's
first name is {0}",
homer.FirstName);
- }
- }
The functionality between my simple example and what is
shown above is the same. The difference is that while I can
easily extend my example using the Properties approach, it
will take some code rewriting to do the same in the simpler
example using public fields.
For example, the following is something that cannot be
done using the public field approach:
- public
int
Age
- {
- get
- {
- if
(age
>
30)
- {
- return
age
*
2;
- }
- else
- {
- return
age
/
2;
- }
- }
- set
- {
- age
=
value;
- }
- }
What is the great is that when you are using Properties,
you don't modify how you set or access any data from
the struct instance. I would set the age property as
homer.Age = 36, and I would get the age by accessing
homer.Age without the assignment ( = ) operator. The get/set
keywords take care of assigning or displaying values without
you doing anything differently.
The main takeaway message of this section is that you
should use Properties when you can, for even though it has a
higher initial cost, the benefits you gain from improved
extensibility and controlling access to an object's internal
state (encapsulation) is definitely worth it.
Just a final word before we wrap up. If you have a question and/or want to be part of a friendly, collaborative community of over 220k other developers like yourself, post on the forums for a quick response!
|