自定义集合类

        我们可以通过实现System.Collections.IEnumerable

System.Collections.IEnumerator接口写出自己需要的集合类。

下面写两种设计实现自定义Library集合类的方法:


 
using System;
using System.Collections;

namespace Library
自定义集合类 - 脆弱的rohelm - 脆弱的rohelm(C入门历程)
 
{
    class Library : IEnumerable//实现接口IEnumerabler
    {
        protected class Enumerator : IEnumerator//实现接口IEnumerator
        {
            private string exceptionInfo =
                "索引无效,MoveNext当前指向索引为空";
            private int index = -1;//指向集合顶部对象的前一位
            private ArrayList books = new ArrayList();//制定一个存放书名的数据成员
            public Enumerator(ArrayList books)
            {
                foreach (string book in books)
                {
                    this.books.Add(book);
                }
                Reset();
            }
            public bool MoveNext()//实现方法MoveNext
            {
                return  (++index < books.Count);    
            }
            public void Reset()//实现方法Reset
            {
                index = -1;
            }
            public object Current//实现属性Current
            {
                get
                {
                    if (0 <= index && index<books.Count)
                        return books[index];//返回当前元素
                    else
                        throw new IndexOutOfRangeException(exceptionInfo);
                }
            }
        }
        protected ArrayList books;
        public Library()
        {
            books = new ArrayList();
        }
        public void Add(string book)
        {
            books.Add(book);
        }
        public void Remove(string book)
        {
            books.Remove(book);
        }
        public IEnumerator GetEnumerator()//实现方法 GetEnumerator
        {
            return new Enumerator(books);
        }
    }
    class Test
    {
        static void Main()
        {
            Library lib = new Library();
            lib.Add("《广告》");
            lib.Add("《传播学》");
            lib.Add("《传媒研究》");
            lib.Add("《市场调研》");
            Console.WriteLine("说含有的课本包括:");
            IEnumerator e = lib.GetEnumerator();
            while (e.MoveNext())
            {
                Console.WriteLine(e.Current);
            }
            Console.ReadKey();
        }
    }
}
////////////////////////////////method of second///////////////////////////////////
using System;
using System.Collections;

namespace Library2
{
    class Library:IEnumerable,IEnumerator
    {
        private int index = -1;
        protected ArrayList books;
        private string exceptionInfo = "索引无效,MoveNext当前指向索引为空";
        public Library()
        {
            books = new ArrayList();
        }
        //实现IEnumerator接口的方法
        public bool MoveNext()
        {
            return (++index < books.Count);
        }
        public void Reset()
        {
            index = -1;
        }
        public object  Current
        {
            get
            {
                if (0 <= index && index < books.Count)
                {
                    return books[index];
                }
                else
                    throw new IndexOutOfRangeException(exceptionInfo);
            }
        }
        //实现IEnumerable接口方法
        public IEnumerator GetEnumerator()
        {
            Reset();
            return this;
        }
         //  添加Library集合类方法
        public void Add(string book)
        {
            books.Add(book);
        }
        public void Remove(string book)
        {
            books.Remove(book);
        }
    }
    class Test
    {
        static void Main()
        {
            Library lib = new Library();
            lib.Add("《广告》");
            lib.Add("《传播学》");
            lib.Add("《传媒研究》");
            lib.Add("《市场调研》");
            Console.WriteLine("说含有的课本包括:");
            foreach (string book in lib)
            {
                Console.WriteLine(book);
            }
            Console.ReadKey();
        }
    }
}
原文地址:https://www.cnblogs.com/rohelm/p/2384096.html