由於該屬性不支持set
,因此我期待在編譯時轉至this.GetProp
失敗。這段代碼編譯得很好,VS2015 & NET461。自動實現的屬性是否有隱含的集合?
public class Example
{
public Example(string val)
{
this.GetProp = val;
this.GetSetProp = val;
}
public string GetProp { get; }
public string GetSetProp { get; set; }
}
在自動屬性中是否隱含對後臺字段的私有訪問?
你可以在構造函數中設置屬性值,即使它消氣而已,就像你可以分配給'readonly'變量 –