Foreach原理

 

如果要被foreach遍历,就必须要实现IEnumerable接口

接口里面只有一个IEnumerator Get Enumerator()方法

但这个方法的返回值要是一个实现了IEnumerator接口的对象

IEnumerator接口中有一个属性 2个方法需要实现

MoveNext方法是用来将指针变量下移,并且判断当前位置是否有值 如果有值返回true否则false

Current属性是用来获取当前指针的位置的值

因为取值是在枚举器中取的,所以要将数组对象传递到枚举器中  在构造函数

View Code
  1 using System;
  2 using System.Collections;
  3 using System.Collections.Generic;
  4 using System.Linq;
  5 using System.Text;
  6 using System.Threading.Tasks;
  7 
  8 namespace 我的动态数组
  9 {
 10     class Program
 11     {
 12         static void Main(string[] args)
 13         {
 14             MyArrayList myML = new MyArrayList(10);
 15             for (int i = 0; i < 8; i++)
 16             {
 17                 myML.Add(i);
 18             }
 19             Console.WriteLine("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
 20             foreach (var item in myML)
 21             {
 22                 Console.WriteLine(item);
 23             }
 24             Console.WriteLine("****************************************");
 25             IEnumerator tor = myML.GetEnumerator();
 26             while (tor.MoveNext())
 27             {
 28                 Console.WriteLine(tor.Current);
 29             }
 30             Console.WriteLine("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
 31 
 32             for (int i = 0; i < myML.Arr.Length; i++)
 33             {
 34                 Console.WriteLine(myML.Arr[i]);
 35             }
 36             
 37             Console.ReadKey();
 38         }
 39     }
 40     class MyArrayList:IEnumerable
 41     {
 42         int[] arr;
 43         int index = 0;
 44         public MyArrayList(int num)
 45         {
 46            arr = new int[num];
 47         }
 48         public int[] Arr
 49         {
 50             get { return arr; }
 51         }
 52         public int this[int index]
 53         {
 54             get{return arr[index];}
 55             set{arr[index]=value;}
 56             
 57         }
 58         public void Add(int r)
 59         {
 60             if (index < arr.Length)
 61             {
 62                 arr[index] = r;
 63                 Console.WriteLine(arr[index]);
 64                 index++;
 65             }
 66             else
 67             {
 68                 int[] newArr = new int[arr.Length * 2];
 69                 arr.CopyTo(newArr, 0);
 70                 arr = newArr;
 71                 arr[index] = r;
 72                 index++;
 73             }
 74  
 75         }
 76 
 77 
 78         public IEnumerator GetEnumerator()
 79         {
 80             return new MyEnumerator(arr, index);
 81         }
 82     }
 83     class MyEnumerator:IEnumerator
 84     {
 85         private int[] irr;
 86         int index=0;
 87         int count = 0;
 88         public MyEnumerator(int[] rr,int count)
 89         {
 90             this.irr = rr;
 91             this.count = count;
 92         }
 93         public object Current
 94         {
 95             get
 96             {
 97                 return irr[index++];
 98             }
 99         }
100 
101         public bool MoveNext()
102         {
103             if (index < count)//index是从0开始,所以应该是小于
104             {
105                 return true;//只要不超过数组的有效下限,就可以认为有值
106             }
107             else
108             {
109                 return false;
110             }
111             
112         }
113 
114         public void Reset()
115         {
116             index = 0;
117         }
118     }
119 }

 

posted on 2012-12-16 00:30  陈谨  阅读(165)  评论(0编辑  收藏  举报