c#实现Foreach枚举_IEnumerable和IEnumerator

来源:互联网 发布:铁矿石港口库存数据 编辑:程序博客网 时间:2024/06/02 02:25

IEnumerable

   public class Indexer //: IEnumerable    {        static public int size = 10;        private string[] indexerList = new string[size];        //构造函数赋初值        public Indexer()        {            for (int i = 0; i < indexerList.Length; i++)                indexerList[i] = "DefaultValue";        }        //c# 索引器        public string this[int index]        {            get            {                if (index < 0 || index > size)                    return "";                return indexerList[index] == null ? string.Empty : indexerList[index].ToString();            }            set            {                if (index < 0 || index > size)                    throw new Exception("超出索引范围: 0~9");                indexerList[index] = value;            }        }        //实现foreach枚举        public IEnumerator GetEnumerator()        {            return this.indexerList.GetEnumerator();        }    }


IEnumerable<string>

   public class IndexerIEnumerable : IEnumerable<string>    {        static public int size = 10;        private string[] indexerList = new string[size];        //索引器        public string this[int index]        {            get            {                if (index < 0 || index > size)                    return "";                return indexerList[index] == null ? string.Empty : indexerList[index].ToString();            }            set            {                if (index < 0 || index > size)                    throw new Exception("超出索引范围: 0~9");                indexerList[index] = value;            }        }        //实现接口IEnumerable枚举器        IEnumerator IEnumerable.GetEnumerator()        {            return GetEnumerator();        }        // 实现接口IEnumerable<out T>的枚举器        public IEnumerator<string> GetEnumerator()        {            foreach (string strTmp in indexerList)                yield return strTmp;            yield break;        }    }

IEnumerator

public class IndexerIEnumerator : IEnumerator    {        static public int size = 10;        private string[] indexerList = new string[size];        //索引器        public string this[int index]        {            get            {                if (index < 0 || index > size)                    return "";                return indexerList[index] == null ? string.Empty : indexerList[index].ToString();            }            set            {                if (index < 0 || index > size)                    throw new Exception("超出索引范围: 0~9");                indexerList[index] = value;            }        }        //索引器枚举时对应的 键值        private int position = -1;        //实现IEnumerator的方法        public object Current        {            get            {                try                {                    return indexerList[position];                }                catch                {                    return null;                }            }        }        public bool MoveNext()        {            position++;            return position < indexerList.Length;        }        public void Reset()        {            position = -1;        }        //实现枚举        public IEnumerator GetEnumerator()        {            foreach (string strTmp in indexerList)                yield return strTmp;            yield break;        }    }

调用

 static class Program    {        [STAThread]        static void Main(string[] args)        {            //索引器测试 2016.7.11            //接口 :IEnumerable 可注释,只要实现GetEnumerator            // 摘要:            //     公开枚举器,该枚举器支持在非泛型集合上进行简单迭代。            //[ComVisible(true)]            //[Guid("496B0ABE-CDEE-11d3-88E8-00902754C43A")]            //public interface IEnumerable             Indexer id = new Indexer();            id[0] = "0";            id[1] = "1";            id[6] = "6";            foreach (string strTmp in id)                Console.WriteLine(strTmp);            Console.ReadKey();            // IEnumerable<string>            // 摘要:            //     公开枚举器,该枚举器支持在指定类型的集合上进行简单迭代。            //            // 类型参数:            //   T:            //     要枚举的对象的类型。             //[TypeDependency("System.SZArrayHelper")]            //public interface IEnumerable<out T> : IEnumerable             IndexerIEnumerable id2 = new IndexerIEnumerable();            id2[0] = "0";            id2[1] = "1";            id2[6] = "6";            foreach (string strTmp in id2)                Console.WriteLine(strTmp);            Console.ReadKey();            // 摘要:            //     支持对非泛型集合的简单迭代。            //[ComVisible(true)]            //[Guid("496B0ABF-CDEE-11d3-88E8-00902754C43A")]            //public interface IEnumerator             IndexerIEnumerator id3 = new IndexerIEnumerator();            id3[0] = "0";            id3[1] = "1";            id3[6] = "6";            foreach (string strTmp in id3)                Console.WriteLine(strTmp);            Console.ReadKey();        }    }

小结:

1,IEnumerable和IEnumerator可以用来实现类的Foreach枚举;

2,IEnumerable :公开枚举器,该枚举器支持在非泛型集合上进行简单迭代。

进行了初步的封装,甚至不需要实现接口,只要实现接口方法:

  //实现foreach枚举        public IEnumerator GetEnumerator()        {            return this.indexerList.GetEnumerator();        }
3,IEnumerator:支持对非泛型集合的简单迭代。

对象是非泛型集合,粒度更细化,要实现的方法也多些.



0 0