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

Implement an interface consuming and returning the same interface in C#

I am having trouble implementing an interface with C#:

    public interface IFooable
    {
        public (IFooable left, IFooable right) FooWith(IFooable other);
    }

And then in my concrete class, I have

class Concrete
{
        public (Concrete left, Concrete right) FooWith(Concrete other)
        {
            return GoFooSomewhereElse(...);
        }
}

But in my tests, the compiler tells me that Concrete does not implement the FooWith function.

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

I don’t really understand what is going on, since I am so new at C# and easily confused by the language!

I found similar questions (eg Why can't I override my interface methods?) but they did not solve my problem.

>Solution :

Try this trick with generic:

public interface IFooable<T>
{
    public (T left, T right) FooWith(T other);
}

class Concrete : IFooable<Concrete>
{
    public (Concrete left, Concrete right) FooWith(Concrete other)
    {
        return GoFooSomewhereElse(...);
    }
}
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