Benjamin
Benjamin

Reputation: 291

Implementing generic interfaces

I have written this code.

public interface IMusicInstrument
{
  string InstrumentType(int InstrumentId);                
}

public interface IGuitar<T, K> where T : GuitarBase where K : IMusicInstrument
{
    string Name { get; set; }
    string GetType(T t);
}

public class ElectricGuitar : GuitarBaseExtended, IGuitar<ElectricGuitar, IMusicInstrument>
{
    public string Name { get; set; }
    public string GetType(ElectricGuitar t)
    {
        return "The electric guitar is: " + t.Name;
    }
}

I was thinking that because the IMusicInstrument requires that I implement a method called InstrumentType that when I include it as a parameter in my IGuitar interface which is implemented on hte ElectricGuitar class that I would get a compile time error. However, I do not.

Am I implementing it wrong?

My goal is to implement the IMusicInstrument into the generic typed IGuitar interface so that both the requirements from IMusiInstrument and IGuitar are enforced.

Any ideas?

Upvotes: 2

Views: 340

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500495

You haven't said that ElectricGuitar implements IMusicInstrument... you've just said that it implements IGuitar<ElectricGuitar, IMusicInstrument>, and that doesn't have the InstrumentType method. It's not clear why you've got the K type parameter in IGuitar<T, K> at all, given that it's not used within the interface.

Upvotes: 1

Related Questions