简体   繁体   中英

How to extend interface c#?

I have an interface in c#, something like this:

public interface IMyInterfaceA 
{
        string Name { get; }
        int Id { get; set; }
}

I want to extend this interface to include additional property.

for example:

public interface IMyInterfaceB: IMyInterfaceA
{
        string newProp { get; }
       
}

The problem is that such syntax is not valid. It required me to implement IMyInterfaceA, so my interface will look like:

public interface IMyInterfaceB: IMyInterfaceA
{
        string newProp { get; }
         public string Vendor => throw new NotImplementedException();

        public int VendorId { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
}

All my goal to use inhertence is so that I wouldn't need to have those properties again.

The IMyInterfaceA is a core inteface I cannot change.

How can I implement an extension but without rewrite the base properties?

There is no problem in your code example. Interfaces support inheritance from another interfaces without implementation of it's members. Maybe your example is not the same as the code where problem occur.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM