Why C# compiler does not allows private property setters in interfaces?

后端 未结 1 1863
春和景丽
春和景丽 2020-12-16 11:31

In certain scenario like a MVVM view-model, I sometimes needs to have private setter as the view-model exposes a state that can only be modified internally.

So is th

相关标签:
1条回答
  • 2020-12-16 11:49

    By definition, an interface is a contract for other code to use, not for private members. However, you can specify read-only properties in interfaces and implement a private setter in the concrete class:

    public interface IFoo
    {
        string MyReadonlyString { get; }
    } 
    
    public class FooImplementation : IFoo
    {
        public string MyReadonlyString { get; private set; }
    }
    
    0 讨论(0)
提交回复
热议问题