Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can I not assign a List of concrete types to a List of that concrete's interface?

Tags:

c#

Why does this not compile?

public interface IConcrete { }

public class Concrete : IConcrete { }

public class Runner
{
    public static void Main()
    {
        var myList = new List<Concrete>();
        DoStuffWithInterfaceList(myList);  // compiler doesn't allow this
    }

    public static void DoStuffWithInterfaceList(List<IConcrete> listOfInterfaces) { }

}

And what's the quickest way to get myList to the correct type?

EDIT I messed up the DoStuffWithInterfaceList example

like image 273
JeremyWeir Avatar asked Nov 20 '09 19:11

JeremyWeir


2 Answers

The accepted solution is quite inefficient for large lists, and completely unnecessary. You can change the signature of your method ever so slightly to make the code work without any conversions, either implicit or explicit:

public class Runner
{
    public static void Main()
    {
        var myList = new List<Concrete>();
        DoStuffWithInterfaceList(myList);  // compiler doesn't allow this
    }

    public static void DoStuffWithInterfaceList<T>(List<T> listOfInterfaces)
        where T: IConcrete
    { }
}

Notice that the method is now generic and uses a type constraint to ensure that it can only be called with lists of IConcrete subtypes.

like image 153
Konrad Rudolph Avatar answered Sep 19 '22 13:09

Konrad Rudolph


Almost all these answers say that this will be supported in C# 4. They are all wrong.

Just to be crystal clear: this is not an example of covariance that we will be supporting in C# 4, because doing so would not be typesafe. We are supporting typesafe covariance and contravariance of generic interfaces and delegates which are constructed with reference type arguments. The example here uses a class type, List, not an interface type. And the interface type, IList, is not typesafe for covariance or contravariance.

IEnumerable will be covariant, as it is an interface that is safe for covariance.

like image 27
Eric Lippert Avatar answered Sep 23 '22 13:09

Eric Lippert