Uco*_*dia 21 c# compiler-construction setter properties interface
在某些情况下,比如MVVM视图模型,我有时需要私有setter,因为视图模型公开了一个只能在内部修改的状态.
那么在接口上需要私有的setter是错误的吗?(我的意思不是特别在所描述的场景中)如果没有,为什么C#编译器不允许它?
谢谢.
Fis*_*rdo 56
根据定义,接口是其他代码使用的合同,而不是私有成员的合同.但是,您可以在接口中指定只读属性,并在具体类中实现私有setter:
public interface IFoo
{
string MyReadonlyString { get; }
}
public class FooImplementation : IFoo
{
public string MyReadonlyString { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10018 次 |
| 最近记录: |