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;
}
}
Related videos on Youtube
Comments
-
Sergej Popov almost 2 years
I have interfaces
IChild
andIParent
.IParent
has a member that is aList<IChild>
.I wish to have classes that implement
IParent
where each class has a member that implementsIChild
: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 almost 12 yearsIt would be wiser to declare your
List
properties asIList
s, they would be less retrictive but offer the same functionality. Doesn't help with the question though. -
nawfal almost 12 yearsthen he can make it just
IEnumerable
. That may not be the requirement -
Suncat2000 over 5 years@Jodrell
IList
does not offer the same functionality asList
. -
Jodrell over 5 years@Suncat2000, you are correct but,
IList
,ICollection
(thier readonly counterparts) orIEnumerable
might be a smaller promise to make that does meet the required level of functionality.
-
-
Rawling almost 12 yearsNot sure why this was up-voted. This won't compile. You can't convert a
List<ChildX>
to or from aList<IChild>
. -
Rawling almost 12 yearsDo you mean, change the property declarations on
ParentA
andParentB
? The one onIParent
is already aList<IChild>
. -
mclark1129 almost 12 yearsYes, you will have to change the declarations on your implementing classes.
-
levininja almost 9 yearsI really wish I could upvote this 100 times...this is a case study on why generics are so useful.
-
OfirD almost 8 yearsCan you elaborate about the rationale behind the solution?