如何使用派生的特定List <TUser>设置基本成员通用List <T>

gwi*_*dry 0 c# generics generic-list

尝试在此派生类中设置通用基本集合类成员时,我遇到编译器错误.

error CS0029: Cannot implicitly convert type 'System.Collections.Generic.List<IntSegment>' to 'System.Collections.Generic.List<T>'

这是我的通用集合的大纲

public class Path<T> : IEnumerable<T> where T : Segment
{
    private List<T> segments = new List<T>();

    public List<T> Segments
    {
        set { segments = value; }
        get { return this.segments; }
    }

    public Path()
    {
        this.Segments = new List<T>();
    }

    public Path(List<T> s)
    {
        this.Segments = s;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,为Segment的派生类IntSegment定义此集合的派生泛型类(为其定义基本集合)

public class IntersectionClosedPath<T> : Path<T>, IEnumerable<T> where T : IntSegment
{
    public IntersectionClosedPath(List<IntSegment> inEdges)
        : base()
    {
        Segments = inEdges;
    }
}
Run Code Online (Sandbox Code Playgroud)

我不明白为什么不允许这项任务.(我不需要制作传入列表的深层副本).

Tim*_* S. 5

更改List<IntSegment> inEdgesList<T> inEdges,它会工作.问题是,Segments被称为一个List<T>,where T : IntSegment同时inEdges是一个List<IntSegment>.(由于我不会进入这里的原因,除非你提出要求,否则不允许这样的任务.如果你有兴趣,请查看方差/协方差/逆变.)