【发布时间】:2013-01-29 13:45:45
【问题描述】:
在 Andrew Tolson 的 Pro C# 中,作者说当非泛型类扩展泛型基类时,派生类必须指定类型参数。
// Assume you have created a custom
// generic list class.
public class MyList<T>
{
private List<T> listOfData = new List<T>();
}
// Non-generic classes must specify the type
// parameter when deriving from a
// generic base class.
public class MyStringList : MyList<string>
{}
我不明白为什么这是必要的?
【问题讨论】:
-
如果没有在派生类中明确指定类型参数,编译器应该如何猜测
T的类型是什么? -
您会提出什么替代方案?