On Jun 14, 2:24 pm, Beorne <matteo...@gmail.comwrote:
In the classes I develop my attributes are always private and are
exposed using properties. From within the class is more correct
to access the attributes directly or to access the attributes using
the properties?
Does "wrapper" setter/getter properties introduce time overhead in
execution?
(I'll assume you mean fields or instance variables when you say
attributes - attributes are something else in .NET/C#.)
If your properties are "straight-through" ones which do absolutely
nothing, there shouldn't be any performance hit.
As for whether you should use the fields directly, that depends on
what you're doing. I personally like to do that as it means that if I
add validation at a later date, my private changes will still be
validated. However, there are times you expressly *don't* want to do
this. For instance, suppose you had two fields, first and last, and
your properties made sure that first was never bigger than last. You
might have a SetFirstAndLast method to set them both at the same time,
which would reasonably use the fields directly (or field then
property) to avoid difficulties where setting either property first
would, in some cases, fail validation.
Jon