Why simple properties?

  • Thread starter Thread starter pinkfloydhomer
  • Start date Start date
Okay. Seems like kind of an random implementation artifact, and not
some deep language reason.

Not a language reason - a design reason. What data a class chooses to
provide access to is part of the encapsulated interface. How it chooses
to store that data *should* be a hidden implementation detail. Exposing
a field exposes that implementation detail.
In situations where you just want people to be able to access the data
(in simple structs only for holding data etc.), exactly the situations
in which you would make "simple properties" as I called them, and in
which you are fairly sure that this will never change, properties are
just syntactic clutter, IMO. That might change with C# 3.0.

"Syntactic clutter" wouldn't change behaviour though, would it?
Changing a field to a property or vice versa certainly can.
 
This has nothing to do with proper encapsulation. Both solutions has
exactly the same degree of encapsulation.

No, they don't. Using properties, I'm hiding the implementation details
of how data is stored - or *if* it is even stored. Some properties may
be computable, for instance, or the value could be cached. If you
expose a field, you're tying the API to the fact that there's a field
of that type to store the value.
Also, property names are just as much an implementation detail as is
an variable name.

No - when all variables are private, and the properties are public, the
property names are part of the interface but the variable names are
just part of the implementation. I can change the implementation
without changing the public interface.
 
But that is precisely because of the seemingly random implementation
choice that properties must be something different than fields. I
can't see why it wouldn't be a better solution to say that a property
has the same interface as a field. Then, if the above situation
happened, you could silently change your fields to properties and
change the type of the underlying fields. I can't see how it is not a
weakness and a random implementation artifact, that this is not
possible.

If all fields are actually properties, how could you then actually
*store* anything?

The difference between properties and fields is that properties act as
*access* and fields act as *storage*. They're not the same thing, and I
don't see any reason to treat them as the same thing.
 

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