Cannot implement interface member because it does not have the matching return type of List

Cannot implement interface member because it does not have the matching return type of List

I have interfaces IChild and IParent. IParent has a member that is a List.
I wish to have classes that implement IParent where each class has a member that implements IChild:
public interface IChild
{ 
}  

public interface IParent
{  
    List a { get; set; }
} 

public class ChildA : IChild
{ 
} 

public class ChildB : IChild
{ 
} 

public class ParentA : IParent
{ 
    public List a { get; set; }
}

public class ParentB : IParent
{ 
    public List a { get; set; }
}

But, this code will not compile. The error is:
`MyApp.Data.ParentA` does not implement interface member `MyApp.Data.IParent.a`.
`MyApp.Data.ParentA.a` cannot implement `MyApp.Data.IParent.a` because it does not have
the matching return type of `System.Collections.Generic.List`.

Solutions/Answers:

Answer 1:

Make IParent generic:

public interface IChild
{
}

public interface IParent<TChild> where TChild : IChild
{
    List<TChild> a { get; set; } 
}

public class ChildA : IChild {  }   

public class ChildB : IChild {  }   

public class ParentA : IParent<ChildA>
{
    public List<ChildA> a { get; set; }
}

public class ParentB : IParent<ChildB>
{
    public List<ChildB> a { get; set; }
}

Answer 2:

You need to have the classes return a List<IChild>:

public class ParentA : IParent
{ 
    public List<IChild> a { get; set; }
}

public class ParentB : IParent
{ 
    public List<IChild> a { get; set; }
}

Answer 3:

The implementation can only return List of IChild as follows:

public interface IChild
{
}

public interface IParent
{
    List<IChild> Children { get; set; }
}

public class ChildA : IChild
{
}

public class ChildB : IChild
{
}

public class ParentA : IParent
{

    public List<IChild> Children
    {
        get;
        set;

    }
}

public class ParentB : IParent
{
    public List<IChild> Children
    {
        get;
        set;
    }
}

Answer 4:

A collection of IChild cannot be implicitly converted to a collection of its child type

Change the return type of IParent.a to List<ChildA> OR change the property declaration on ParentA and ParentB to public List<IChild> a { get; set; }. I recommend the latter, as I think that is what you’re most likely going for.

Answer 5:

I had a similar requirement where I had two different methods that operated on two different classes but had the same logic in it for the properties that are common to both the classes.

so I thought to use inheritance and generics for this to write a common method, I was able to achieve in the following way.

namespace OOPS.Interfaces
{
    using System.Collections.Generic;

    public interface IBanner
    {
        string Name { get; set; }
    }

    public interface IBannerContent<T> where T : IBanner
    {
        List<T> Banners { get; set; }
    }
}

Simple Model.

namespace OOPS.Simple
{
    using Interfaces;
    using System.Collections.Generic;

    public class Banner : IBanner
    {
        public string Name { get; set; }
    }

    public class BannerContent : IBannerContent<Banner>
    {
        public List<Banner> Banners { get; set; }
    }
}

Complex Model.

namespace OOPS.Complex
{
    using Interfaces;
    using System.Collections.Generic;

    public class Banner : IBanner
    {
        public string Name { get; set; }

        public string Description { get; set; }
    }

    public class BannerContent : IBannerContent<Banner>
    {
        public List<Banner> Banners { get; set; }
    }
}

The common business logic and sample invocation. The key part here is using the where clause to constrain the type such as where T : IBanner all the way down till the method we want it to be common.

namespace OOPS
{
    using Interfaces;
    using System;
    using System.Collections.Generic;

    public class BusinessLogic
    {
        public void Print<T>(IBannerContent<T> bannerContent) where T : IBanner
        {
            foreach (var item in bannerContent.Banners)
            {
                Console.WriteLine(item.Name);
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var banner1 = new Simple.BannerContent
            {
                Banners = new List<Simple.Banner>
                {
                    new Simple.Banner { Name = "Banner 1" },
                    new Simple.Banner { Name = "Banner 2" }
                }
            };

            var banner2 = new Complex.BannerContent
            {
                Banners = new List<Complex.Banner>
                {
                    new Complex.Banner { Name = "Banner 3", Description = "Test Banner" },
                    new Complex.Banner { Name = "Banner 4", Description = "Design Banner" }
                }
            };

            var business = new BusinessLogic();
            business.Print(banner1);
            business.Print(banner2);
            Console.ReadLine();
        }
    }
}

Our Awesome Tools

References