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

17,665

Solution 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; }
}

Solution 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; }
}

Solution 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;
    }
}
Share:
17,665

Related videos on Youtube

Sergej Popov
Author by

Sergej Popov

SOreadytohelp

Updated on July 03, 2022

Comments

  • Sergej Popov
    Sergej Popov almost 2 years

    I have interfaces IChild and IParent. IParent has a member that is a List<IChild>.

    I wish to have classes that implement IParent where each class has a member that implements IChild:

    public interface IChild
    { 
    }  
    
    public interface IParent
    {  
        List<IChild> a { get; set; }
    } 
    
    public class ChildA : IChild
    { 
    } 
    
    public class ChildB : IChild
    { 
    } 
    
    public class ParentA : IParent
    { 
        public List<ChildA> a { get; set; }
    }
    
    public class ParentB : IParent
    { 
        public List<ChildB> 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<MyApp.Data.IChild>`.
    
    • Jodrell
      Jodrell almost 12 years
      It would be wiser to declare your List properties as ILists, they would be less retrictive but offer the same functionality. Doesn't help with the question though.
    • nawfal
      nawfal almost 12 years
      then he can make it just IEnumerable. That may not be the requirement
    • Suncat2000
      Suncat2000 over 5 years
      @Jodrell IList does not offer the same functionality as List.
    • Jodrell
      Jodrell over 5 years
      @Suncat2000, you are correct but, IList, ICollection (thier readonly counterparts) or IEnumerable might be a smaller promise to make that does meet the required level of functionality.
  • Rawling
    Rawling almost 12 years
    Not sure why this was up-voted. This won't compile. You can't convert a List<ChildX> to or from a List<IChild>.
  • Rawling
    Rawling almost 12 years
    Do you mean, change the property declarations on ParentA and ParentB? The one on IParent is already a List<IChild>.
  • mclark1129
    mclark1129 almost 12 years
    Yes, you will have to change the declarations on your implementing classes.
  • levininja
    levininja almost 9 years
    I really wish I could upvote this 100 times...this is a case study on why generics are so useful.
  • OfirD
    OfirD almost 8 years
    Can you elaborate about the rationale behind the solution?