为什么不能列出<parent> = List <child>?

rye*_*guy 1 .net c# oop inheritance

为什么以下代码不起作用?

class parent {}
class kid:parent {}

List<parent> parents=new List<kid>;
Run Code Online (Sandbox Code Playgroud)

这对我来说似乎很明显.这里发生了什么?

jas*_*son 10

C#目前不支持协方差.

它的未来在.NET 4.0中,然而,在接口和委托.

Eric Lippert在他的博客上有一篇关于这个主题的精彩系列.Visual Studio Magazine在最近的一篇文章中也介绍了它.

  • +1:Liskov替换原则的经典失败.拟议的儿童通常不能代替拟议的父母,因此继承可能是错误的. (3认同)