Let’s say I have a class that exposes one property. Is it considered to be a good aproach to use the private ‘holder variable’ for internal use in the class? Or should I use the property for internal use also.
To explain, should I use:
public class foo { String _statusHolder; public String myStaus { get { return _statusHolder; } set{ _statusHolder = value; } } public void DisplayMyStatus() { Console.WriteLine(_statusHolder); } }
Or:
public class foo { String _statusHolder; public String myStaus { get { return _statusHolder; } set{ _statusHolder = value; } } public void DisplayMyStatus() { Console.WriteLine(myStaus); } }
I could see it as beeing more consistent and more readable to use the second approach. It would also be more effective if I would later do some modificatins in the set-statement. But are there any performance issues or is it considered bad-practise for some reason?
EDIT:
It seems that everybody is leaning towards using the property internally. My initial thoughts was the same, but as a novice programmer, you can never know. Thanks everyone for the quick feedback!
I tend to go with calling the properties cause once stuff gets tricky you can put in locking and business logic in the getter
For C# 3.0 I would go with something along these lines (and only explicitly create the backing field when its really needed)