为什么这个接口实现不起作用?

ano*_*non 2 c# inheritance interface

我似乎有一个接口实现的基本语法问题.基本上我有这个:

    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)
        {

        }
    }
Run Code Online (Sandbox Code Playgroud)

在我看来,ConcreteObject工具IMarkerInterface因此ConcreteDoStuff.DoStuff()应该实施IDoStuffInterface.

但是我收到了编译错误 "Error ConcreteDoStuff does not implement interface IDoStuffInterface.DoStuff()"

怎么会?

jti*_*ley 5

您实现的方法需要与接口具有完全相同的签名.虽然所有'ConcreteObject'对象都是'IMarkerInterface'类型,但并非所有'IMarkerInterfaces'都是'ConcreteObject'.因此,这两个签名并不相同.接口必须能够保证CLR该类型的任何对象都实现了有效的方法.