在包含它们的类中编写代码时,使用私有字段或属性是一种好习惯吗?
例如,如果我有这个字段/属性对,则此类之外的类必须使用该属性.课堂内的代码怎么样?它应该使用私人领域,还是应该通过财产?
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?
}
Run Code Online (Sandbox Code Playgroud)
在这里使用属性的一个优点是,如果getter中有任何逻辑,它仍然会被执行.我很想知道这里是否有最佳实践政策.