.NET Casting Generic List
Can someone explain to me why in .NET 2.0 if I have an interface, IPackable
and a class that implements that interface OrderItem
, when I have a method that takes in a List<IPackable>
, passing in a list of List<OrderItem>
does not work?
Does anyone know how I could accomplish this functionality?
Code:
public interface IPackable {
double Weight{ get; }
}
public class OrderItem : IPackable
public List<IShipMethod> GetForShipWeight(List<IPackable> packages) {
double totalWeight = 0;
foreach (IPackable package in packages) {
totalWeight += package.Weight;
}
}
The following code does not work.
List<OrderItem> orderItems = new List<OrderItem>();
List<IShipMethod> shipMethods = GetForShipWeight(orderItems);
Solution 1:
JMD is half correct. In fact, it's absolutely incorrect to say that we will be able to cast a generic list with C# 4.0. It's true that covariance and contravariance will be supported in C# 4.0 but it will only works with interface and delegate and there will have a lot of constraints. Therefore, it won't work with List
.
The reason is really simple.
If B is a subclass of A, we cannot say that List<B>
is a subclass of List<A>
.
And here's why.
List<A>
exposes some covariances methods (returning a value) and some contravariances methods (accepting a value as a parameter).
e.g.
-
List<A>
exposesAdd(A);
-
List<B>
exposesAdd(B);
If List<B>
inherits from List<A>
...than you would be able to do List<B>.Add(A);
Therefore, you would loose all type safety of generics.
Solution 2:
The feature is called covariance/contravariance and will be supported in c# 4.0. You can read about it here: http://blogs.msdn.com/ericlippert/archive/tags/Covariance+and+Contravariance/default.aspx
Solution 3:
JMD's answer is correct. For a workaround, you can try this:
List<IPackable> orderItems = new List<IPackable>();
List<IShipMethod> shipMethods = GetForShipWeight(orderItems);
Or, if the list must be strongly typed as OrderItems, then this (3.0 only, sorry):
List<IShipMethod> shipMethods =
GetForShipWeight(orderItems.Cast<IPackable>().ToList());