c#中的实现迭代器模式

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections;
  6. namespace CSharp基础
  7. {
  8.     class 迭代器
  9.     {
  10.         public static void Main()
  11.         {
  12.             MyArrayList arr = new MyArrayList();
  13.             foreach (int i in arr)
  14.             {
  15.                 Console.WriteLine( i );
  16.             }
  17.             Console.Read();
  18.         }
  19.     }
  20.     class MyArrayList : IEnumerable
  21.     {
  22.         public IEnumerator GetEnumerator()
  23.         {
  24.             return new MyEnumerator();
  25.         }
  26.         private class MyEnumerator : IEnumerator
  27.         {
  28.             private int _current = -1;
  29.             int[] item = { 22, 33, 44, 23, 33, 5, 2, 3 ,66,44 ,44,33,22,22,343,3434};
  30.             public object Current
  31.             {
  32.                 get 
  33.                 {
  34.                     return this.item[_current];
  35.                 }
  36.             }
  37.             public bool MoveNext()
  38.             {
  39.                 this._current++;
  40.                 return (_current < item.Length);
  41.             }
  42.             public void Reset()
  43.             {
  44.                 this._current = -1;
  45.             }
  46.         }
  47.     }
  48. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值