I seem to be having a basic syntax problem with an interface implementation. Basically I have this:
public interface IMarkerInterface
{
}
public class ConcreteObject : IMarkerInterface
{
}
public interface IDoStuffInterface
{
void DoStuff(IMarkerInterface obj);
// also doesn't work
// void DoStuff<T>(T obj) where T : IMarkerInterface;
}
public class ConcreteDoStuff : IDoStuffInterface
{
public void DoStuff(ConcreteObject c)
{
}
}
To my mind, ConcreteObject implements IMarkerInterface, so therefore ConcreteDoStuff.DoStuff() should implement IDoStuffInterface.
But I get a compilation error "Error ConcreteDoStuff does not implement interface IDoStuffInterface.DoStuff()"
How come?
Your implemented methods need to have the exact same signature as the interface. While all 'ConcreteObject' objects are of type 'IMarkerInterface', not all 'IMarkerInterfaces' will be 'ConcreteObject'. Thus the two signatures are not equivalent. Interfaces must be able to guarantee the CLR that any object of that type has a valid method implemented.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With