AutoImplemented Properties

  • Thread starter Thread starter Duggi
  • Start date Start date
D

Duggi

As I read, one of the advantage of implementing auto-Implemented
Properties is to avoid the accidental access/ modification of the
private variable in the class.

However If set or get needs to be customized(as per the class logic),
there is no option I see, other than writing the private variable
again.

In which case, it can only be useful for the extendibility of public
variables to be converted into properties in the future.

I see no advantage over the public variables.

Is there any other major factor, which drove the implementation of
auto-Implemented properties in .Net3.0 ???

Thanks in advance

-Srinivas
 
You seem to believe that you should have auto-implemented properties, or
public fields, but not regular public properties with backing variables such
as below:

private string _name;
public string Name {
get {return _name;}
set {_name = value;}
}

A reason for properties is that they can be used for databinding in
controls, where fields cannot, to the best of my knowledge.
 
As I read, one of the advantage of implementing auto-Implemented
Properties is to avoid the accidental access/ modification of the
private variable in the class.
However If set or get needs to be customized(as per the class logic),
there is no option I see, other than writing the private variable
again.
In which case, it can only be useful for the extendibility of public
variables to be converted into properties in the future.
I see no advantage over the public variables.
Is there any other major factor, which drove the implementation of
auto-Implemented properties in .Net3.0 ??? [...]

You'd have to ask the language designers to be sure what their main goal  
was.  But it seems to me you've identified the main reason for having  
auto-implemented properties: encouraging developers to write  
"future-resistant" code (there's no such thing as "future-proof" :) ).

That is, auto-implemented properties make it that much simpler to write  
things that act like properties as actual properties.  Then in the future,  
if a more complex implementation is needed, it can be added without  
necessarily having to recompile all the dependent code.

Pete

Thanks Pete. I was checking if I was on the right learning track or
not.

Thanks for the reply.

-Cnu
 
Back
Top