定义
1using System;
2using System.Collections;
3
4namespace Relaction.Collections
5{
6 /**//// <summary>
7 ///
8 /// </summary>
9 public class MyEnumerator:IEnumerator
10 {
11 private int _index;
12 private string[] _list;
13 public MyEnumerator(string[] list)
14 {
15 _list = list;
16 }
17 IEnumerator 成员#region IEnumerator 成员
18
19 public void Reset()
20 {
21 _index = 0;
22 }
23
24 public object Current
25 {
26 get
27 {
28 return _list[_index];
29 }
30 }
31
32 public bool MoveNext()
33 {
34 _index++;
35 if(_index >= _list.Length)
36 {
37 _index = _list.Length-1;
38 return false;
39 }
40 return true;
41 }
42
43 #endregion
44
45 }
46 public class MyEnumerable:IEnumerable
47 {
48 private string[] _list;
49 public MyEnumerable()
50 {
51 _list = new string[3];
52 _list[0] = "1";
53 _list[1] = "2";
54 _list[2] = "3";
55 }
56 IEnumerable 成员#region IEnumerable 成员
57
58 public IEnumerator GetEnumerator()
59 {
60 return new MyEnumerator(_list);
61 }
62
63 #endregion
64 }
65}
66
1using System;
2using System.Collections;
3
4namespace Relaction.Collections
5{
6 /**//// <summary>
7 ///
8 /// </summary>
9 public class MyEnumerator:IEnumerator
10 {
11 private int _index;
12 private string[] _list;
13 public MyEnumerator(string[] list)
14 {
15 _list = list;
16 }
17 IEnumerator 成员#region IEnumerator 成员
18
19 public void Reset()
20 {
21 _index = 0;
22 }
23
24 public object Current
25 {
26 get
27 {
28 return _list[_index];
29 }
30 }
31
32 public bool MoveNext()
33 {
34 _index++;
35 if(_index >= _list.Length)
36 {
37 _index = _list.Length-1;
38 return false;
39 }
40 return true;
41 }
42
43 #endregion
44
45 }
46 public class MyEnumerable:IEnumerable
47 {
48 private string[] _list;
49 public MyEnumerable()
50 {
51 _list = new string[3];
52 _list[0] = "1";
53 _list[1] = "2";
54 _list[2] = "3";
55 }
56 IEnumerable 成员#region IEnumerable 成员
57
58 public IEnumerator GetEnumerator()
59 {
60 return new MyEnumerator(_list);
61 }
62
63 #endregion
64 }
65}
66
客户:
客户
1 private void button6_Click(object sender, System.EventArgs e)
2 {
3 Relaction.Collections.MyEnumerable m = new Relaction.Collections.MyEnumerable();
4 foreach(string s in m)
5 {
6 label1.Text += s.ToString();
7 }
8
9 }
1 private void button6_Click(object sender, System.EventArgs e)
2 {
3 Relaction.Collections.MyEnumerable m = new Relaction.Collections.MyEnumerable();
4 foreach(string s in m)
5 {
6 label1.Text += s.ToString();
7 }
8
9 }