// Declare the generic class public class GenericList<T> { void Add(T input) { } } class TestGenericList { private class ExampleClass { } static void Main() { // Declare a list of type int GenericList<int> list1 = new GenericList<int>();
// Declare a list of type string GenericList<string> list2 = new GenericList<string>();
// Declare a list of type ExampleClass GenericList<ExampleClass> list3 = new GenericList<ExampleClass>(); } }
//方法1:
public class MyCollection1:List<MyItem>
{
public new void Add(MyItem item)
{
if(this.Contains(item)) return;
base.Add(item);
}
}
//方法1:
public class MyCollection2:IList<MyItem>
{
private List<MyItem> innerList = new List<MyItem>();
public void Add(MyItem item)
{
if(this.Contains(item)) return;
base.Add(item);
}
//实现n多的方法后,附带的,有个Add方法,实现即可,实现方法略
}
------解决方案--------------------