C# - Cannot implicitly convert type List to List

前端 未结 6 1510
生来不讨喜
生来不讨喜 2020-11-27 11:46

I have a project with all my Interface definitions: RivWorks.Interfaces
I have a project where I define concrete implmentations: RivWorks.DTO

I\'ve done this hu

6条回答
  •  半阙折子戏
    2020-11-27 12:22

    Yep it's a covariance limitation in C#. You can't convert a list of one type to a list of another.

    Instead of:

    List myList = new List();
    

    You have to do this

    List myList = new List();
    
    myList.Add(new dto.Product());
    

    Eric Lippert explains why they implemented it this way: http://blogs.msdn.com/ericlippert/archive/tags/Covariance+and+Contravariance/default.aspx

    (And why it is different than working with arrays of items).

提交回复
热议问题