Interface inheritance: is extending properties possible?

后端 未结 7 1191
庸人自扰
庸人自扰 2021-02-03 23:32

I want to do this:

interface IBase
{
    string Property1 { get; }
}

interface IInherited : IBase
{
    string Property1 { get; set; }
}

So th

7条回答
  •  梦如初夏
    2021-02-04 00:06

    Not explicitly, no. You have two options:

    public interface IBase
    {
        string Property1 { get; }
    }
    
    public interface IInherited : IBase
    {
        void SetProperty1(string value);
    }
    

    Or you can just kill the compiler warning with the new keyword:

    public interface IBase
    {
        string Property1 { get; }
    }
    
    public interface IInherited : IBase
    {
        new string Property1 { get; set; }
    }
    

    Unless you implement IInherited.Property1 explicitly, IBase will bind to your settable implementation automatically.

提交回复
热议问题