Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

   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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading