【发布时间】:2012-11-29 00:51:24
【问题描述】:
给定以下代码:
namespace sample
{
class a { }
class b : a { }
public class wrapper<T> { }
class test
{
void test1()
{
wrapper<a> y = new wrapper<b>();
//Error 11 Cannot implicitly convert type 'sample.wrapper<sample.b>' to 'sample.wrapper<sample.a>'
}
}
}
从逻辑上讲,因为b 是a,所以wrapper<b> 是wrapper<a>。那为什么我不能进行这种转换,或者我该怎么做呢?
谢谢。
【问题讨论】:
-
可能是因为 b 是 a 的孩子。如果你说
wrapper<b> y = new wrapper<a>;会发生什么? -
@FlorisPrijt 我得到了同样的错误。
-
b是a但wrapper<b>不一定是wrapper<a>。 stackoverflow.com/a/1779278/284240
标签: c#