自定义Collection类最快的一种方式:继承CollectionBase抽象类


CollectionBase类提供了一套可以实现构造自身群集的抽象方法集合。
CollectionBase 类还提供了一种基础的数据结构——InnerList(一个 ArrayList) 。此结构可以用作自身类的基础。

 

using System.Collections命名空间之下:

 

 

namespace MyCollectionBase
{
    using System.Collections;

class Program { static void Main(string[] args) { myCollection c = new myCollection(); c.Add("DSC"); c.Add("CSC"); c.Add("BSC"); c.Add("ASC"); foreach (string item in c) { Console.WriteLine(item); } Console.WriteLine(c.Length()); Console.ReadKey(); } } class myCollection : CollectionBase { public void Add(object item) { InnerList.Add(item); } public int Length() { return InnerList.Count; } public void Delete(object item) { InnerList.Remove(item); } public void Clear() { InnerList.Clear(); } } }

 

posted on 2015-11-17 11:05  巴夫巴夫  阅读(252)  评论(0编辑  收藏  举报