6

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);
1
  • Please post the relevant code and what specific trouble you're having (build error, runtime error, etc).
    – Misko
    Commented Mar 23, 2009 at 18:50

5 Answers 5

14

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> exposes Add(A);
  • List<B> exposes Add(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.

11

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

1
  • 2
    Article seems to have moved. Look below for examples, the Cast<> class is in the System.Linq namespace just FYI.
    – Lucas
    Commented May 7, 2013 at 3:41
7

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

An alternative solution that also works for .NET 3.5

List<IShipMethod> shipMethods = GetForShipWeight(orderItems).ConvertAll(sm => sm as IShipMethod);
2

Actually, you can solve this by making GetForShipWeight a generic function:

public interface IPackable { double Weight { get; } }
public interface IShipMethod { }

public class OrderItem : IPackable { }

public List<IShipMethod> GetForShipWeight<T>(List<T> packages) where T : IPackable
{
    List<IShipMethod> ship = new List<IShipMethod>();
    foreach (IPackable package in packages)
    {
        // Do something with packages to determine list of shipping methods
    }
    return ship;
}
public void Test()
{
    List<OrderItem> orderItems = new List<OrderItem>();
    // Now compiles...
    List<IShipMethod> shipMethods = GetForShipWeight(orderItems);
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.