Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't this interface implementation work?

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?

like image 868
anon Avatar asked May 12 '26 00:05

anon


1 Answers

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.

like image 63
jtimperley Avatar answered May 14 '26 14:05

jtimperley