【发布时间】:2009-10-26 17:15:34
【问题描述】:
不能重载 List 的 Add 方法吗?
class ListDemo<T>:List<T>
{
public override T Add<T>(T value)
{
return base.Add(value);
}
}
我收到以下错误:
1) 类型参数“T”与外部类型“CollectionsExample.ListDemo”中的类型参数同名
2) 'CollectionsExample.ListDemo.Add(T)': 找不到合适的方法来覆盖
【问题讨论】:
-
你为什么要继承
List<T>而不是仅仅实现IList<T>(并且支持组合而不是继承)? -
@Daniel Pryden。谢谢。我是初学者。向你们学习。
-
@Daniel:您在项目中重新实现了多少次
IList<T>接口?如果您只想覆盖列表中的Add或Remove等方法,System.Collections.ObjectModel.Generic.Collection<T>几乎总是更好的选择。
标签: c# collections