Search code examples
c#asp.netinterfacegetter-setter

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


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?


Solution

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