Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Possible Duplicate:
Private variable vs property?

I have a public property with a get and set accessor. Associated with this property is a private field.

Is it best practice when referencing these values in other parts of a classes code to use the private field or the public exposed property. Although I release I have made a private bool personFieldSpecified field when I probably didn't need to I did this to better explain my question.

        private SimplePerson personField;
        private bool personFieldSpecified;

        public SimplePerson Person
        {
            get
            {
                if (personField == null)
                    personField = new SimplePerson();

                return personField;
            }

            set
            {
                personField = value;

                // Q)  Should I access the public property here or the private field
                PersonSpecified = true;
            }
        }

        public bool PersonSpecified
        {
            get
            {
                return personFieldSpecified;
            }

            set
            {
                personFieldSpecified = value;
            }
        }

Because I'm aware of the inner implementation of the set I know that it will effect the end result if I set either, however I am wondering which is the better practice to do for long term maintainability. This could apply to methods of my class as well!

What would be the reasons for choosing one particular method over the other. Does it depend on what the property itself is doing in which case I have to have knowledge of it's inner workings or does it come down to design?

share|improve this question

marked as duplicate by pdr, Karl Bielefeldt, Walter, jmort253, Yannis Rizos Mar 13 '12 at 13:12

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
@pdr Yes it is thanks. I did do a search but couldn't find a question on this. The duplicate question has answered mine. –  dreza Mar 12 '12 at 23:04
add comment

1 Answer

If a property exists for a field, I would recommend always using it to access the property even within the class. This will help prevent you introducing bugs in future if you do add other logic in the property getter or setter which you would then bypass if you directly accessed the field.

The rule I follow is if a property exists for a field, only access the field from the class construtor or the property. I'd even go so far as to add a private setter to ensure that any 'set' logic is still encapsulated against further change.

The only time to go against this is if you have a field which is not exposed outside the class (e.g. a bool field to determine whether the class has been disposed).

share|improve this answer
    
Thanks Trevor. It appears my question is a duplicate of programmers.stackexchange.com/questions/133015/… but what you said makes sense. –  dreza Mar 12 '12 at 23:05
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.