Private Properties

Posted on 2005-04-06
Last Modified: 2010-04-16
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
Question by:gamesmeister
    LVL 8

    Accepted Solution

    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 :)
    LVL 96

    Expert Comment

    by:Bob Learned
    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;

    LVL 4

    Author Comment

    Thanks for the responses.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now