How do you implement a private setter when using an interface?

不想你离开。 提交于 2019-11-29 20:54:22

In interface you can define only getter for your property

interface IFoo
{
    string Name { get; }
}

However, in your class you can extend it to have a private setter -

class Foo : IFoo
{
    public string Name
    {
        get;
        private set;
    }
}

Interface defines public API. If public API contains only getter, then you define only getter in interface:

public interface IBar
{
    int Foo { get; }    
}

Private setter is not part of public api (as any other private member), thus you cannot define it in interface. But you are free to add any (private) members to interface implementation. Actually it does not matter whether setter will be implemented as public or private, or if there will be setter:

 public int Foo { get; set; } // public

 public int Foo { get; private set; } // private

 public int Foo 
 {
    get { return _foo; } // no setter
 }

 public void Poop(); // this member also not part of interface

Setter is not part of interface, so it cannot be called via your interface:

 IBar bar = new Bar();
 bar.Foo = 42; // will not work thus setter is not defined in interface
 bar.Poop(); // will not work thus Poop is not defined in interface
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!