C# - Cannot implicitly convert type List to List

前端 未结 6 1508
生来不讨喜
生来不讨喜 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:37

    You can't do that. If you have a List, you can put any IProduct in it. So if you have a Product2 which implements IProduct you could put it in the list. But the original list was created as List, so anyone using the list would expect only objects of type Product, not Product2 to be in the list.

    In .NET 4.0, they added covariance and contravariance for interfaces, so you could convert IEnumerable to IEnumerable. But this still doesn't work for lists, since the list interface allows you both to "put stuff in" and "get stuff out".

提交回复
热议问题