Interface inheritance: is extending properties possible?

后端 未结 7 1199
庸人自扰
庸人自扰 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:10

    Unfortunately not - properties cannot be extended as such. However, you can just hide the property by using new:

    interface IInherited : IBase
    {
        // The new is actually unnecessary (you get warnings though), hiding is automatic
        new string Property1 { get; set; }
    }
    

    Or, you can make your own getter and setter methods which can be overriden (good 'ol Java style):

    interface IBase
    {
        string GetProperty1();
    }
    interface IInherited : IBase
    {
        void SetProperty1(string str);
    }
    

    Properties are actually converted to getter and setter methods by the compiler.

提交回复
热议问题