【发布时间】:2017-08-09 12:06:29
【问题描述】:
由于该属性不支持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; }
}
自动属性中是否隐含对支持字段的私有访问?
【问题讨论】:
-
您可以在构造函数中设置属性的值,即使它是 getter-only,就像您可以分配给
readonly变量一样
标签: c#