【发布时间】:2011-06-11 04:12:56
【问题描述】:
我收到以下错误
`System.Collections.Generic.List>.Add(MyContainer)' 的最佳重载方法匹配有一些无效参数 (CS1502) (GenericsTest)
对于以下类:
A 和 B 是 MyBase 的子类。
public class GenericConstraintsTest
{
private MyList<MyContainer<MyBase>> myList = new MyList<MyContainer<MyBase>>();
public GenericConstraintsTest ()
{
MyContainer<A> ca = new MyContainer<A>(new A());
this.Add<A>(new A());
this.Add<B>(new B());
}
public void Add<S> (S value) where S : MyBase
{
MyContainer<S> cs = new MyContainer<S>(value);
myList.Add(cs);
}
public static void Main()
{
GenericConstraintsTest gct = new GenericConstraintsTest();
}
}
我做错了什么?
干杯
【问题讨论】:
标签: c# generics collections