- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Collections;
- namespace CSharp基础
- {
- class 迭代器
- {
- public static void Main()
- {
- MyArrayList arr = new MyArrayList();
- foreach (int i in arr)
- {
- Console.WriteLine( i );
- }
- Console.Read();
- }
- }
- class MyArrayList : IEnumerable
- {
- public IEnumerator GetEnumerator()
- {
- return new MyEnumerator();
- }
- private class MyEnumerator : IEnumerator
- {
- private int _current = -1;
- int[] item = { 22, 33, 44, 23, 33, 5, 2, 3 ,66,44 ,44,33,22,22,343,3434};
- public object Current
- {
- get
- {
- return this.item[_current];
- }
- }
- public bool MoveNext()
- {
- this._current++;
- return (_current < item.Length);
- }
- public void Reset()
- {
- this._current = -1;
- }
- }
- }
- }
c#中的实现迭代器模式
最新推荐文章于 2024-08-08 20:23:02 发布