What is the best practice for calling members/fields from a private method and public method? Should the private method always call private fields or should they call the public members?
private string _name;
public string Name
{
get {return _name; }
set { _name = value; }
}
public void DoSomething()
{
_doSomething();
}
private void _doSomething()
{
_name.ToLower();
}
In some cases your public property might contain some logic which you need and in that case you will always use the property instead of the local variable, if you are sure that you want to use the private member variable, and not expose that functionality to the outside world, make that particular method private.
I prefer to have all code go through the public interface, simply to reduce the number of places in the code that accesses the actual backing field. Two reasons are
Or, to put it in a single word: encapsulation.