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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With