Re: Simpler syntax for properties?

From: Jon Skeet [C# MVP] (skeet_at_pobox.com)
Date: 04/07/04


Date: Wed, 7 Apr 2004 20:28:38 +0100

Julie <julie@nospam.com> wrote:
> > If you want to later change the implementation, you can't.
> > If you want to limit access to read or write only, you can't.
> > If you want to validate values on set, you can't.
> > If you want to trace use during debug, you can't.
>
> If I want to later change the implementation, I could simply do this:
>
> private SomeType p; // Was: P
>
> public SomeType P
> {
> get { /* ... */ }
> set { /* ... */ }
> }
>
> That solves 1 and 2, right?

No - you've just broken your public interface, so anything using it
needs to be recompiled. Using properties, the public interface stays
the same even if the implementation changes.
 
> The OP didn't indicate the need to validate values to trace in debug. If those
> are requirements, then you are right. If not requirements, then what is wrong
> w/ my idea?

The above. If the OP's requirements for tracing, validation etc change
(as they often do), using properties provides a backward-compatible way
of working. Fields don't.

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


Relevant Pages

  • Re: Simpler syntax for properties?
    ... > If you want to limit access to read or write only, ... The OP didn't indicate the need to validate values to trace in debug. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: how to handle errors and preconditions
    ... > I have an interface that looks like this: ... > void validate; ... lines when I have debug on. ...
    (comp.lang.java.help)