c#实现Foreach枚举_IEnumerable和IEnumerator

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:支持对非泛型集合的简单迭代。

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



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值