C# Inheritance – Why List Cannot Be Assigned to List

.netc++inheritanceoop

Why won't the following code work?

class parent {}
class kid:parent {}

List<parent> parents=new List<kid>;

It seems obvious to me. What's going on here?

Best Answer

C# does not currently support covariance.

It's coming in .NET 4.0, however, on interfaces and delegates.

Eric Lippert had a very nice series on this subject on his blog awhile back. Visual Studio Magazine covers it too in a recent article.

Related Question