自定义Collections
定义:

 定义
定义
1 using System;
using System;  
2 using System.Collections;
using System.Collections;
3
4 namespace Relaction.Collections
namespace Relaction.Collections
5

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

 定义
定义1
 using System;
using System;  2
 using System.Collections;
using System.Collections;3

4
 namespace Relaction.Collections
namespace Relaction.Collections5


 {
{6

 /**//// <summary>
    /**//// <summary>7
 ///
    /// 8
 /// </summary>
    /// </summary>9
 public class MyEnumerator:IEnumerator
    public class MyEnumerator:IEnumerator10

 
     {
{11
 private int _index;
        private int _index;12
 private string[] _list;
        private string[] _list;13
 public MyEnumerator(string[] list)
        public MyEnumerator(string[] list)14

 
         {
{15
 _list = list;
            _list = list;16
 }
        }17

 IEnumerator 成员#region IEnumerator 成员
        IEnumerator 成员#region IEnumerator 成员18

19
 public void Reset()
        public void Reset()20

 
         {
{21
 _index = 0;
            _index = 0;22
 }
        }23

24
 public object Current
        public object Current25

 
         {
{26
 get
            get27

 
             {
{28
 return _list[_index];
                return _list[_index];29
 }
            }30
 }
        }31

32
 public bool MoveNext()
        public bool MoveNext()33

 
         {
{34
 _index++;
            _index++;35
 if(_index >= _list.Length)
            if(_index >= _list.Length)36

 
             {
{37
 _index = _list.Length-1;
                _index = _list.Length-1;38
 return false;
                return false;39
 }
            }40
 return true;
            return true;41
 }
        }42

43
 #endregion
        #endregion44

45
 }
    }46
 public class MyEnumerable:IEnumerable
    public class MyEnumerable:IEnumerable47

 
     {
{48
 private string[] _list;
        private string[] _list;49
 public MyEnumerable()
        public MyEnumerable()50

 
         {
{51
 _list = new string[3];
            _list = new string[3];52
 _list[0] = "1";
            _list[0] = "1";53
 _list[1] = "2";
            _list[1] = "2";54
 _list[2] = "3";
            _list[2] = "3";55
 }
        }56

 IEnumerable 成员#region IEnumerable 成员
        IEnumerable 成员#region IEnumerable 成员57

58
 public IEnumerator GetEnumerator()
        public IEnumerator GetEnumerator()59

 
         {
{60
 return new MyEnumerator(_list);
            return new MyEnumerator(_list);61
 }
        }62

63
 #endregion
        #endregion64
 }
    }65
 }
}66

客户:

 客户
客户1
 private void button6_Click(object sender, System.EventArgs e)
    private void button6_Click(object sender, System.EventArgs e)2

 
         {
{3
 Relaction.Collections.MyEnumerable m = new Relaction.Collections.MyEnumerable();
            Relaction.Collections.MyEnumerable m = new Relaction.Collections.MyEnumerable();4
 foreach(string s in m)
            foreach(string s in m)5

 
             {
{6
 label1.Text += s.ToString();
                label1.Text += s.ToString();7
 }
            }8

9
 }
        }.jpg) 
  
 
                    
                     
                    
                 
                    
                
 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号