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

dotnetnoob picture dotnetnoob · Aug 15, 2013 · Viewed 35.2k times · Source

I've created an interface with some properties.

If the interface didn't exist all properties of the class object would be set to

{ get; private set; }

However, this isn't allowed when using an interface,so can this be achieved and if so how?

Answer

Rohit Vats picture Rohit Vats · Aug 15, 2013

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;
    }
}