抽象类中的属性可见性

Vin*_*nce 3 c# properties

有人知道关于在抽象类或父类中公共属性后面定义属性可见性(私有或受保护)的方式的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包含更多的代码,它可能不是一个好习惯...

你觉得怎么样?

谢谢.

Tam*_*ege 9

非const字段应该总是私有的.如果由于某些原因无法使用自动属性而需要使用字段,请确保它是私有的.儿童班应通过公共或受保护的物业进入.