有人知道关于在抽象类或父类中公共属性后面定义属性可见性(私有或受保护)的方式的C#最佳实践.
在其他世界中,默认情况下(以及为什么)之间的最佳实践是:
public abstract class MyClass
{
private string myAttribute;
public string MyAttribute
{
get { return myAttribute; }
set { myAttribute = value; }
}
}
Run Code Online (Sandbox Code Playgroud)
和
public abstract class MyClass
{
protected string myAttribute;
public string MyAttribute
{
get { return myAttribute; }
set { myAttribute = value; }
}
}
Run Code Online (Sandbox Code Playgroud)
我认为子类应该有办法直接处理这个受保护的属性,但如果getter或setter包含更多的代码,它可能不是一个好习惯...
你觉得怎么样?
谢谢.