Search code examples
inheritanceinterfaceaccess-modifiers

inheriting an interface and setting access-modifiers


Would like to do "private set;". Is there any alternative?

public interface IFoo
{
    IEnumerable data { get;  set; }

}

public class Foo : IFoo
{
    public IEnumerable data
    {
        get;
        private set;
    }

}

Solution

  • You can remove the set accessor from the interface:

    public interface IFoo
    {
        IEnumerable data { get; }
    
    }
    

    Or you can implement the interface as explicit, but then you will need to somehow implement the set method:

    public class Foo : IFoo
    {
    
        public IEnumerable data
        {
            get;
            private set;
        }
    
        IEnumerable IFoo.data
        {
            get { return data; }
            set { throw new NotSupportedException(); }
        }
    }