By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,997 Members | 1,098 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,997 IT Pros & Developers. It's quick & easy.

Properties vs Get Set Accessor methods

P: n/a
Why is it considerd best practice to use Properties rather than Get and Set
accessor methods?
-Tim Sprout
Feb 1 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Tim Sprout <tm**@ptialaska.netwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 1 '07 #2

P: n/a
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.
Feb 1 '07 #3

P: n/a
On Feb 1, 8:34 am, "Tim Sprout" <t...@ptialaska.netwrote:
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.

Feb 1 '07 #4

P: n/a
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;
}
}
"Tim Sprout" <tm**@ptialaska.netwrote in message
news:12*************@corp.supernews.com...
Why is it considerd best practice to use Properties rather than Get and
Set
accessor methods?
-Tim Sprout


Feb 1 '07 #5

P: n/a
Dan Reber <dr****@nospam.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 1 '07 #6

P: n/a
Great, thanks.

Dan

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
Dan Reber <dr****@nospam.comwrote:
>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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Feb 1 '07 #7

P: n/a
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.

--
Regards,
Robson Siqueira
Enterprise Architect
"Tim Sprout" <tm**@ptialaska.netwrote in message
news:12*************@corp.supernews.com...
Why is it considerd best practice to use Properties rather than Get and
Set
accessor methods?
-Tim Sprout


Feb 1 '07 #8

P: n/a
Why is it considerd best practice to use Properties rather than Get and
Set
accessor methods?
-Tim Sprout
Great responses. Thanks.

-Tim Sprout

Feb 2 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.