简体   繁体   中英

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

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

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.

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.

Currently, this is forbidden because otherwise the type safety would be broken. you could do something like this inside of DoStuffWithInterfaceList:

public class OtherConcrete : IConcrete { }

public void DoStuffWithInterfaceList(List<IConcrete> listOfInterfaces) 
{
       listOfInterfaces.Add(new OtherConcrete ());
}

Which will fail at run time because listOfInterfaces is of type Concrete only.

As others said, this will be possible is C# 4 as long as you don't change the list inside the method but you'll have to explicitly tell it to the compiler.

To answer your other question about converting the list, If you're using .Net 3.5 I would go with Enumerable.Cast<> extension method. otherwise, you can write a lazy conversion method yourself using the yield keyword, which will give you the same effect.

EDIT:

As Eric Lippert said, you should use IEnumerable in order for it to work in C# 4.

C# does not currently support converting generic types like that ( it will be supported in C# 4, if I understand it correctly As wcoenen states in comments below, and Eric also clarifies in his answer, the only way to make it work in C#4 is to use IEnumerable<IConcrete> ). For now you will need to convert your list in some way.

You could call the method like this:

DoStuffWithInterface(myList.ConvertAll<IConcrete>(n => n as IConcrete));

Update
I realized that you probably don't need the cast inside the lambda, even though I sort of like it for clarity. So this should also work:

DoStuffWithInterface(myList.ConvertAll<IConcrete>(n => n));

This has to do with covariance and contravariance. Eric Lippert wrote a lot about it earlier this year. (11 blog entries specifically on that topic.) The first one is Covariance and Contravariance in C#, Part One . Read that and search his blog for the rest of them. He provides detailed explanations of why this kind of thing is difficult.

Good news: some of the restrictions are lifted in C# 4.0.

IList wouldn't work because IList isn't contravariant. It needs to be IEnumerable though again this only works in 4.0. You could also just use a ConvertAll with a lambda expression and that will work in 3.5

You could try

public void DoStuffWithInterface(IList<IConcrete> concrete) { }

but I think this only works in .NET 4.0.

If you wanna be dirty just do

public void DoStuffWithInterface(IList concrete) { }

and check to see if the objects coming out are concrete.

foreach (var item in myList)
    DoStuffWithInterface(item);

or

public void DoStuffWithInterface(IList<IConcrete> concrete) { }

or

var myNewList = myList.Cast<IConcrete>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM