views:

1350

answers:

3

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, passing in a list of List does not work?

Does anyone know how I could accomplish this functionality?

Thanks Josh

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);

+6  A: 

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

JMD
+1  A: 

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());
mquander
+2  A: 

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 wont 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 there's why.

List esposes some covariances methods(returning a value) and some contravariances methods(accepting a value as a parameter).

e.g.

List<A> esposes Add(A); List<B> exposes Add(B);

If List inherits from List<A>...than you would be able to do List<B>.Add(A);

Therefore, you would loose all type safety of generics.