Foreach 实现遍历方法

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Collections;
  6 
  7 namespace _06为什么有些类可以使用foreach循环遍历
  8 {
  9     class Program
 10     {
 11         static void Main(string[] args)
 12         {
 13             //ArrayList arrList1 = new ArrayList() { 20, 3, 49, 39, 48 };
 14             //foreach (var item in arrList1)
 15             //{
 16             //    Console.WriteLine(item);
 17             //}
 18             //Console.WriteLine("=========================================");
 19 
 20             //string[] names = { "a", "b", "c", "d" };
 21             //foreach (string item in names)
 22             //{
 23             //    Console.WriteLine(item);
 24             //}
 25 
 26             //Console.WriteLine("====================================");
 27             //List<string> list = new List<string>() { "乔丹", "可比", "詹姆斯" };
 28             //foreach (string item in list)
 29             //{
 30             //    Console.WriteLine(item);
 31             //}
 32 
 33             //Console.WriteLine("====================================");
 34             Person p = new Person();
 35             p[0] = "奇瑞QQ";
 36             p[1] = "infiniti";
 37             p[2] = "阿斯顿马丁";
 38             //for (int i = 0; i < p.Count; i++)
 39             //{
 40             //    Console.WriteLine(p[i]);
 41             //}
 42 
 43             //任何类型,只要想使用foreach来循环遍历,就必须在当前类型中
 44             //存在: public IEnumerator GetEnumerator()方法,(一般情况我们会通过实现IEnumerable接口,来创建该方法。)
 45             foreach (string item in p)
 46             {
 47                 Console.WriteLine(item);
 48             }
 49 
 50             //IEnumerator etor = p.GetEnumerator();
 51             //while (etor.MoveNext())
 52             //{
 53             //    string str = etor.Current.ToString();
 54             //    Console.WriteLine(str);
 55             //}
 56             Console.ReadKey();
 57 
 58 
 59         }
 60     }
 61 
 62     public class Person : IEnumerable
 63     {
 64         private List<string> listCar = new List<string>();
 65 
 66         public int Count
 67         {
 68             get
 69             {
 70                 return this.listCar.Count;
 71             }
 72 
 73         }
 74 
 75         public string this[int index]
 76         {
 77             get
 78             {
 79                 return listCar[index];
 80             }
 81 
 82             set
 83             {
 84                 if (index >= listCar.Count)
 85                 {
 86                     listCar.Add(value);
 87                 }
 88                 else
 89                 {
 90                     listCar[index] = value;
 91                 }
 92             }
 93         }
 94         public string Name
 95         {
 96             get;
 97             set;
 98         }
 99         public int Age
100         {
101             get;
102             set;
103         }
104         public string Email
105         {
106             get;
107             set;
108         }
109 
110         #region IEnumerable 成员
111 
112         //这个方法的作用不是用来遍历的,而是用来获取一个对象
113         //这个对象才是用来遍历的。
114         public IEnumerator GetEnumerator()
115         {
116             return new PersonEnumerator(listCar);
117         }
118 
119         #endregion
120     }
121 
122     //这个类型,的作用就是用来遍历Person中的List集合的。
123     public class PersonEnumerator : IEnumerator
124     {
125         public PersonEnumerator(List<string> _cars)
126         {
127             cars = _cars;
128         }
129 
130         //这个字段中存储的就是Person对象中的listCar集合
131         private List<string> cars;
132 
133 
134         //假设一开始遍历的对象的索引是-1
135         private int index = -1;
136 
137         #region IEnumerator 成员
138 
139 
140         //表示获取当前正在遍历的那个对象
141         public object Current
142         {
143             get
144             {
145                 if (index < 0)
146                 {
147                     return null;
148                 }
149                 return cars[index];
150             }
151         }
152         //让自定义下标index累加
153         public bool MoveNext()
154         {
155             index = index + 1;
156             if (index >= cars.Count)
157             {
158                 return false;
159             }
160             else
161             {
162                 return true;
163             }
164         }
165 
166         public void Reset()
167         {
168             index = -1;
169         }
170 
171         #endregion
172     }
173 }
View Code

 

posted @ 2013-07-24 17:38  墓地de小草  阅读(240)  评论(0编辑  收藏  举报