为什么不能名单,其中亲代GT; =名单,其中,儿童>?亲代、名单、儿童、GT

2023-09-03 07:33:43 作者:Sober (清醒)

为什么不会以下code的工作?

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?

推荐答案

C#目前不支持协方差。

,这是未来在.NET 4.0,但是在接口和委托。

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

埃里克利珀有一个非常漂亮的的系列在他的博客这个主题的一段时间回来。 的Visual Studio杂志涵盖太在最近的文章。

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.