Search code examples
c#.netgenericsinheritanceinterface

Problem with generic base class and not-generic child class in an interface


I'm having some trouble with implementing an interface on a "non-generic" child class, that derives from a generic base class:

    public class Parent<T> where T : struct, Enum
    {
    }

    public class Child : Parent<MyEnum>
    {
    }

    interface ISomeInterface<T> where T : struct, Enum
    {
        void SomeMethod(Parent<T> parent);
    }

    class SomeClass : ISomeInterface<MyEnum>
    {
        public void SomeMethod(Child child)
        {
            return;
        }
    }

It won't compile because "SomeClass does not implement the interface..."

When I change to

   public void SomeMethod(Parent<MyEnum> child)

everything is fine.

But shouldn't the first code also work since Child actually is Parent<MyEnum> ?


Solution

  • I guess you are assuming inheritance in implementing an interface method works the same as calling a method with parent class. They are different.