C# 中的Iterator

迭代器 Iterator

 

我先贴上代码,

 public class CheckBox : IEnumerable<string>
    {
        private string[] strings;
        private int rtc;

        /*
         * 索引
         */
        public string this[int index]
        {
            get
            {
                if (0 > index && index > 7)
                {
                    return null;
                }
                else
                {
                    return strings[index];
                }
            }

        }

        public CheckBox(params string[] InitString)
        {
            rtc = 0;
            strings = new string[8];
            foreach (string s in InitString)
            {
                strings[rtc++] = s;
            }
        }

        public void Add(string theString)
        {
            if (rtc >= 0 && rtc <= 7)
            {
                strings[rtc++] = theString;
            }
        }

        public int GetNumEnties()
        {
            return rtc;
        }

        public void Remove(string key)
        {
            ArrayList temp = new ArrayList();
            temp.AddRange(strings);
            temp.Remove(key);
            strings = new string[8];
            temp.CopyTo(strings);
        }
        public System.Collections.Generic.IEnumerator<string> GetEnumerator()
        {
            foreach (string s in strings)
            {
                yield return s;
            }
        }
        
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            //return (IEnumerator)(new Object());   不推荐强制转换的做法,
            throw new NotImplementedException();
        }

    }


注意啦,一定要实现两个GetEnumerator();  父类子类的 ;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值