Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a generic collection of objects to a method that requires a collection of the base type

Say I have a method that is expecting a generic collection parameter of a base type, see Test.MethodA(IEnumerable(BaseClass) listA) below. How come when I pass it a collection of a derived type the code wont build? Wouldn't all instances of DerivedClass also be a BaseClass?

I could have just created a new List(BaseClass) and passed that to MethodA(IEnumerable(BaseClass) listA). But I would think C# would be smart enough to know that a collection of DerivedClass has all the same properties as a collection of BaseClass.

Is using the List.Cast(T)() method as I've shown the best way to solve this problem?

abstract class BaseClass
{
    public int SomeField;
    public abstract string SomeAbstractField
    {
        get;
    }
}

class DerivedClass:BaseClass
{
    public override string SomeAbstractField
    {
        get { return "foo"; }
    }
}

class TestClass
{ 

    public void MethodA(IEnumerable<BaseClass> listA)
    {

    }

    public void MethodB()
    {
        List<DerivedClass> listB = new List<DerivedClass>();

        //Error 16  The best overloaded method match for 
        //TestClass.MethodA(List<BaseClass>)' 
        //has some invalid arguments
        this.MethodA(listB);

        //this works
        this.MethodA(listB.Cast<BaseClass>());
    }
}
like image 336
Eric Anastas Avatar asked Dec 22 '22 10:12

Eric Anastas


2 Answers

Cast<>() is the best way to solve it at the moment. Your original version would work fine in C# 4.0 / .NET 4.0 though, where IEnumerable<T> is covariant in T.

(I've just verified it compiles under .NET 4.0 beta 1.)

Until .NET 4.0 and C# 4 come out, generics are invariant - IEnumerable<object> and IEnumerable<string> are effectively unrelated interfaces. Even in .NET 4.0, you wouldn't be able to do this with List<T> as the parameter type - only interfaces and delegates will be variant, and even then only when the type parameter is only used in appropriate positions (output positions for covariance, input positions for contravariance).

To learn more about variance in C# 4, read Eric Lippert's excellent series of blog posts about it.

like image 108
Jon Skeet Avatar answered Dec 28 '22 07:12

Jon Skeet


In the general case (i.e. for collections that are modifiable), it is NOT the case that "a collection of DerivedClass has all the same properties as a collection of BaseClass": into a collection of Fruit you can insert Banana, Apple and Pear -- in a collection of Orange, you can't, so, you see, it doesn't have the same properties (under modification).

IEnumerable is a different case, since it's NOT modifiable, as as @Jon says this excessive restriction has been removed for that special case in 4.0.

like image 39
Alex Martelli Avatar answered Dec 28 '22 06:12

Alex Martelli