Home > Mobile >  How to extend interface c#?
How to extend interface c#?

Time:12-10

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 IMyInterfaceAis a core inteface I cannot change.

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

EDIT: There is no bug with my code. I mistakenly created a class not an interface.

CodePudding user response:

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.

  • Related