Is it good practice to use the private field, or the property, when writing code in the class that contains them?
For example, if I have this field/property pair, classes outside this class must use the property. What about code inside the class? Should it use the private field, or should it also go through the property?
private string _foo;
protected string Foo
{
get { return this._foo; }
}
private void SomeMethod()
{
string dummyVariable = "snuh" + this._foo; // So, this...
string dummyVariable = "snuh" + this.Foo; // ... or this?
}
One advantage of using the property here, is if there is any logic in the getter, it will still get executed. I'm curious to know if there is a best-practice policy to follow here.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…