C# – Cannot implicitly convert type List to List

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<contracts.IProduct> myList = new List<dto.Product>();

You have to do this

List<contracts.IProduct> myList = new List<contracts.IProduct>();

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 (archived)

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

Leave a Comment