Why does this not compile?
public interface IConcrete { }
public class Concrete : IConcrete { }
public class Runner
{
public static void Main()
{
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>();
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.
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.
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.
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