为什么不能名单,其中亲代GT; =名单,其中,儿童>? [英] Why can't List<parent> = List<child>?

查看:108
本文介绍了为什么不能名单,其中亲代GT; =名单,其中,儿童>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不会以下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杂志涵盖太在最近的<一个href="http://visualstudiomagazine.com/articles/2009/05/01/generic-covariance-and-contravariance-in-c-40.aspx"相对=nofollow>文章。

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.

这篇关于为什么不能名单,其中亲代GT; =名单,其中,儿童&GT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆