I don't understand why the below code doesn't work in c# (.NET Core 1.1).
public interface IChild
{
}
public interface IParent
{
IEnumerable<IChild> Children { get; set; }
}
public abstract class ParentBase<TChild> : IParent
where TChild : IChild
{
public IEnumerable<TChild> Children {get; set; }
}
The error message is
Error CS0738 'ParentBase< TChild>' does not implement interface member 'IParent.Children'. 'ParentBase< TChild>.Children' cannot implement 'IParent.Children' because it does not have the matching return type of 'IEnumerable< IChild>'.
The error message says the IEnumerable< TChild> is not of type IEnumerable< IChild> while I constrain TChild to IChild.
I found two 'easy' ways out of this, but both require rather bad changes in the client code. I load an entire object tree of these types using ConfigurationSection.Bind() in the client code.
Unwanted solution 1 : Make the parent interface generic
public interface IChild
{
}
public interface IParent<TChild>
where TChild : IChild
{
IEnumerable<TChild> Children { get; set; }
}
public abstract class ParentBas<TChild> : IParent<TChild>
where TChild : IChild
{
public IEnumerable<TChild> Children {get; set; }
}
Unwanted solution 2 :
public interface IChild
{
}
public interface IParent
{
IEnumerable<IChild> Children { get; set; }
}
public abstract class ParentBas<TChild> : IParent
where TChild : IChild
{
public IEnumerable<IChild> Children {get; set; }
}