自定义集合 
//自定义集合,可以存储object类型数据 
    public class MyArray : IEnumerable,IEnumerator 
    { 
        //用于存储数据 
        ArrayList list = new ArrayList(); 
        //用于记录指针或者游标位置 
        int index = -1; 
        //得到IEnumerator 
        public IEnumerator GetEnumerator() 
        { 
            return (IEnumerator)this; 
        } 
        //得到当前对象 
        public object Current 
        { 
            get { return list[index]; } 
        } 
        //游标往后移动一位,如果没有超出list长度,则可以继续遍历,否则遍历结束 
        public bool MoveNext() 
        { 
            index++; 
            return index > list.Count - 1 ? false : true; 
        } 
        //将游标重置 
        public void Reset() 
        { 
            index = -1; 
        } 
        public void Add(object item) 
        { 
            list.Add(item); 
        } 
    } 
//====================================================== 
 
自定义泛型集合 
    //自定义泛型集合 
    public class MyList<T> : IEnumerable<T>, IEnumerator<T>,IDisposable  
    { 
        //创建一个List<T>集合存储对象 
        private List<T> list = new List<T>(); 
        int index = -1; 
        //添加对象 
        public void Add(T item)  
        { 
            list.Add(item); 
        } 
        //因为foreach需要迭代器IEnumerator类型,所以,把当前对象根据里氏转换原则,转换为其父类型 
        public IEnumerator<T> GetEnumerator() 
        { 
            return (IEnumerator<T>)this; 
        } 
        //这里也不清楚 是干嘛的!!! 
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() 
        { 
            return GetEnumerator(); 
        } 
        //获得当前对象 
        public T Current 
        { 
            get {  
                return list[index]; 
            } 
        } 
        //这个是释放对象吧, 
        public void Dispose() 
        { 
            //这里不清楚怎样释放对象资源,我觉得把里面的资源给设为null,然后调用垃圾回收器回收 
            //应该没啥问题了,还好了,这里看自己怎么写合适 
            this.list = null; 
            GC.Collect(); 
        } 
        //...这个没弄明白 ????代码是我自己加的 
        object System.Collections.IEnumerator.Current 
        { 
            get { return Current; } 
        } 
        //根据游标的位置判断是否已经遍历完所有元素,如果没有遍历完,则可以继续遍历 
        public bool MoveNext() 
        { 
            index++; 
            return index > list.Count-1? false : true; 
        } 
        //将游标重置 
        public void Reset() 
        { 
            index = -1; 
        } 
    } 
 |