Private Properties

I'm looking at a piece of code that was written a while ago by an experienced developer, and one thing I notice is that he has used private properties to access his private variables

i.e. He has public accessors to expose certain private variables to the outside world, but he also uses private accessors to expose them internally, so that he never directly reads or writes the private variable.

Is that good practice to do so, or is it an unnecessary layer of encapsulation? If so, what are the reasons?

The code itself is written in C#, but does have some unsafe code blocks as it accesses parts of the Windows API...don't know if that makes any difference?

Many thanks
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Well it could be used for when you want to do something with the value before getting or setting it. But if that is not used, you could say it does not add anything.

That doesn't mean it useless though. Maybe the developer likes to work with properties. Also, an advantage could be that in the rare occassion you'd want to make a variable public it would be easier because you could simply change the accessor of the property. Or, maybe he likes it to make sure he doesn't accidently set the variable in a method. Leaving the set accessor could accomplish this.

Anyway, the extra overhead isn't really an issue :)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bob LearnedCommented:
There is a generalized guidelines for fields and properties as highlighted here:

C# properties

For internal use, it is generally preferred to have fields:

  private int someProperty = 0;)

and then expose that field to the outside world with a property:

public int getSomeProperty()
        return someProperty;

    public void setSomeProperty(int propValue)
        someProperty = propValue;

gamesmeisterAuthor Commented:
Thanks for the responses.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.