Properties vs Get Set Accessor methods

  • Thread starter Thread starter Tim Sprout
  • Start date Start date
T

Tim Sprout

Why is it considerd best practice to use Properties rather than Get and Set
accessor methods?


-Tim Sprout
 
Tim Sprout said:
Why is it considerd best practice to use Properties rather than Get and Set
accessor methods?

Because they're more readable, and because tools which are designed to
use properties will find properties but not accessor methods. Accessor
methods are basically just following a convention - properties also
follow a convention, but have extra metadata declaring them to be
properties.
 
Why is it considerd best practice to use Properties rather than Get and
Set
accessor methods?

Because it's a high-level, built-in feature designed explicitly for this
purpose and therefore much cleaner IMO. Not only do they instantly convey
their intended purpose to other readers (improving your code's legibility),
but they're also specifically recognized by different tools, controls, etc.
For instance, you can pass an object to the native "PropertyGrid" control
and it will automatically populate the control with all properties in your
object.
 
Why is it considerd best practice to use Properties rather than Get and Set
accessor methods?

-Tim Sprout

In colloquial terms, someone hands you a screwdriver. Do you say, "Why
should I use this, when a butter knife can get the job done?"

Properties formalize an informal standard. In essence, the compiler
now supports and enforces rules about something that (in Java, for
example) used to be the programmer's responsibility to get right. I
can't see any downside to that, although I do remember one poster here
who vehemently maintained that properties were crap and that getter
and setter methods were the only way to go. I can't recall why, sorry.
 
This may be a stupid question but am I using (see below) Properties or
Accessor methods? If am using Accessor methods, how do I convert the below
to Properties?

Thanks

Dan


public int ObjectID
{
get
{
return objectID;
}
set
{
objectID = value;
}
}
 
Dan Reber said:
This may be a stupid question but am I using (see below) Properties or
Accessor methods? If am using Accessor methods, how do I convert the below
to Properties?

That's a property. It generates accessor methods behind the scenes, but
it's a property. If you wrote "plain" accessor methods, it would be
something like:

public int GetObjectID()
{
return objectID;
}

public void SetObjectID(int value)
{
objectID = value;
}

The generated accessors have an extra _ in the name, as well as more
metadata.
 
Great, thanks.

Dan

Jon Skeet said:
That's a property. It generates accessor methods behind the scenes, but
it's a property. If you wrote "plain" accessor methods, it would be
something like:

public int GetObjectID()
{
return objectID;
}

public void SetObjectID(int value)
{
objectID = value;
}

The generated accessors have an extra _ in the name, as well as more
metadata.
 
IMHO, I would - and this is what I do most of the time - is to use
properties. Why? I am a crazy "dynamic business rules" kind of person. When
using reflection, for instance, properties are your friend.

Also, properties are more readable than accessor methods, IMO.

Again, it is a personal opinion.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top