> No offense, but I vote for "slow". :) Or at least (and
> maybe more polite), ill-informed.
> > No offense, but I vote for "slow". :) Or at least (and
> > maybe more polite), ill-informed.
[quoted text clipped - 7 lines]
> public int setNumber (int n) {this.number = n;}
> public void getNumber () {return this.number;}
The point of properties is to make syntax simpler, so you can write:
x.Foo.Bar.Baz
instead of
x.getFoo().getBar().getBaz()
That's really all it is.
> but on your reply it sounds that it's basically the same
> as in C++/Java. Do we have "properties" just for the
> same of having something other than "instance
> variables"?
The point of properties is that they expose data as part of the API
without exposing the details of how that data is stored. A field is
part of the implementation of a class - a (non-private) property is
part of the API.
> In that case, i'll be off to kick my friend
> who swore that i shouldn't use "byte _info" to carry
> the contents presented by "Info"-property. :)
Well, if you can use an automatic property, you might as well do so
(and the compiler will autogenerate a field). But if you're writing the
property manually, you should absolutely use a field (unless it could
be a more esoteric property, e.g. one which reads its data from a
file).

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
K Viltersten - 19 Mar 2008 11:11 GMT
>> I was under impression that the whole thing with
>> properties is not to use the setter/getter syntax:
[quoted text clipped - 13 lines]
>
> That's really all it is.
Ah, got it. Thanks.
--
Regards
Konrad Viltersten
--------------------------------
sleep - a substitute for coffee for the poor
ambition - lack of sense to be lazy
Peter Duniho - 19 Mar 2008 18:36 GMT
> [...]
> Well, if you can use an automatic property, you might as well do so
> (and the compiler will autogenerate a field). But if you're writing the
> property manually, you should absolutely use a field (unless it could
> be a more esoteric property, e.g. one which reads its data from a
> file).
I think this is worth emphasizing: I think the phrase "should absolutely"
is misleading, since the examples of "a more esoteric property" are
numerous, and especially since the one example provided seems to imply
that some dedicated state is still stored somewhere.
In fact, anything that represents some well-defined state which can be
either retrieved, set, or both could qualify as a property. A property
need not have any data behind it directly storing the state at all, not
even stored somewhere that's an alternative to a plain old field, like a
database, file, remote process, etc.
Obviously there has to be some state that is affected when setting, and
some state that is inspected when getting, but in neither case does that
state need to be dedicated to the property. I would say that you "should
absolutely use a field" only when the property is a simple "store and
retrieve this data" sort of thing. There are lots of examples of
properties that don't have dedicated storage for their state.
I know that Jon understands this, but it's my feeling that his use of the
word "absolutely" conveys an implication that I'm sure he didn't intend.
His parenthetical qualification of the statement doesn't, I think, go far
enough in acknowledging the many and varied situations in which you would
not have a field dedicated to storing a property's state.
Pete
Jon Skeet [C# MVP] - 19 Mar 2008 18:56 GMT
> > [...]
> > Well, if you can use an automatic property, you might as well do so
[quoted text clipped - 7 lines]
> numerous, and especially since the one example provided seems to imply
> that some dedicated state is still stored somewhere.
Agreed in every respect.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk