About Properties and Fields

  • Thread starter Thread starter Rafael Veronezi
  • Start date Start date
R

Rafael Veronezi

Also about Properties and Fields, I have a simple doubt (looks more like a
recomendation than a doubt)...
In a class implementation with private fields and public properties...
What's the recomendation to access the content of the fields? Not to assing,
just to retrieve... Using the public properties or accessing directly the
Fields?
I mean this, for fields where the associated property returns just the field
value...
Thanks
 
Rafael said:
Also about Properties and Fields, I have a simple doubt (looks more
like a recomendation than a doubt)...
In a class implementation with private fields and public properties...
What's the recomendation to access the content of the fields? Not to
assing, just to retrieve... Using the public properties or accessing
directly the Fields?
I mean this, for fields where the associated property returns just
the field value...
Thanks

In general if you want to give access to data in your class do it through
properties rather than through fields. However, properties are not a
universal panacea, and have their own inherent problems. For example, C#
does not allow you to return a value from a setter, this is an issue because
it prevents the following (where x is a read/write property on class C):

C c1 = new C();
C c2 = new C();
c1.x = c2.x = 10;

C++ does allow you to return a value from a setter. Also note that C# lets
you (lazily) use the same name for the property as the type. An example of
this is Control.Size. This is a bad thing because it upsets the C++ compiler
when you try to access the property. In this .NET world you have to think in
a language agnostic way.

Also, properties can be made readonly by only providing a getter, but be
careful when you do this:

class WebSite
{
private Uri _uri = new Uri(http://www.microsoft.com);
public Uri Address{ get {return _uri;} }
}

Is Address read-only? Well, no because Uri is a class so Address returns a
reference to the field, and hence the user can change the field via this
reference. To make the property really readonly you must make a copy:

public Uri Address
{
get {return new Uri(_uri, "");}
}

While I am ranting about properties there are some other things to remember.
The first thing is that if you have several properties that are associated
it is almost always better to set them with a method call, and get them with
the property getter. Using a setter to set them is often very inefficient.
The Form class is a good example, it has a Location property, a Size
property, Left, Top, Width and Height. These are all associated, and when
you use the setter for any of these it ends up in a call to SetBounds, so if
you change the location and size through properties SetBounds is called many
times. Its more efficient to call SetBounds once.

My final point concerns events: some people fire events in setters and their
should carefully document this. For example, if you have a Left and Top
property you will want to indicate that the location has changed, but
changing both of these properties will raise the event twice!

Richard
 
Back
Top