Strong-Type Collection
自定义集合类:

 自定义强类型集合类
自定义强类型集合类
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 MyStrongTypeList:IList
    public class MyStrongTypeList:IList
10
 
     {
{
11 private ArrayList _list;
        private ArrayList _list;
12 public MyStrongTypeList()
        public MyStrongTypeList()
13
 
         {
{
14 _list = new ArrayList();
            _list = new ArrayList();
15 }
        }
16
 IList 成员#region IList 成员
        IList 成员#region IList 成员
17
18 public bool IsReadOnly
        public bool IsReadOnly
19
 
         {
{
20 get
            get
21
 
             {
{
22 return _list.IsReadOnly;
                return _list.IsReadOnly;
23 }
            }
24 }
        }
25
26 public object this[int index]
        public object this[int index]
27
 
         {
{
28 get
            get
29
 
             {
{
30 return _list[index];
                return _list[index];
31 }
            }
32 set
            set
33
 
             {
{
34 _list[index] = value;
                _list[index] = value;
35 }
            }
36 }
        }
37
38 public void RemoveAt(int index)
        public void RemoveAt(int index)
39
 
         {
{
40 _list.RemoveAt(index);
            _list.RemoveAt(index);
41 }
        }
42
43 void IList.Insert(int index, object value)
        void IList.Insert(int index, object value)
44
 
         {
{
45 _list.Insert(index,value);
            _list.Insert(index,value);
46 }
        }
47
48 public void Insert(int index,string value)
        public void Insert(int index,string value)
49
 
         {
{
50 ((IList)this).Insert(index,value);
            ((IList)this).Insert(index,value);
51 }
        }
52
53 void IList.Remove(object value)
        void IList.Remove(object value)
54
 
         {
{
55 _list.Remove(value);
            _list.Remove(value);
56 }
        }
57 public void Remove(string value)
        public void Remove(string value)
58
 
         {
{
59 ((IList)this).Remove(value);
            ((IList)this).Remove(value);
60 }
        }
61
62 bool IList.Contains(object value)
        bool IList.Contains(object value)
63
 
         {
{
64 return _list.Contains(value);
            return _list.Contains(value);
65 }
        }
66
67 public bool Contains(string value)
        public bool Contains(string value)
68
 
         {
{
69 return ((IList)this).Contains(value);
            return ((IList)this).Contains(value);
70 }
        }
71
72 public void Clear()
        public void Clear()
73
 
         {
{
74 _list.Clear();
            _list.Clear();
75 }
        }
76
77 int IList.IndexOf(object value)
        int IList.IndexOf(object value)
78
 
         {
{
79 return _list.IndexOf(value);
            return _list.IndexOf(value);
80 }
        }
81
82 public int IndexOf(string value)
        public int IndexOf(string value)
83
 
         {
{
84 return ((IList)this).IndexOf(value);
            return ((IList)this).IndexOf(value);
85 }
        }
86
87 int IList.Add(object value)
        int IList.Add(object value)
88
 
         {
{
89 return _list.Add(value);
            return _list.Add(value);
90 }
        }
91
92 public int Add(string value)
        public int Add(string value)
93
 
         {
{
94 return ((IList)this).Add(value);
            return ((IList)this).Add(value);
95 }
        }
96
97 public bool IsFixedSize
        public bool IsFixedSize
98
 
         {
{
99 get
            get
100
 
             {
{
101 return _list.IsFixedSize;
                return _list.IsFixedSize;
102 }
            }
103 }
        }
104
105 #endregion
        #endregion
106
107
 ICollection 成员#region ICollection 成员
        ICollection 成员#region ICollection 成员
108
109 public bool IsSynchronized
        public bool IsSynchronized
110
 
         {
{
111 get
            get
112
 
             {
{
113 return _list.IsSynchronized;
                return _list.IsSynchronized;
114 }
            }
115 }
        }
116
117 public int Count
        public int Count
118
 
         {
{
119 get
            get
120
 
             {
{
121 return _list.Count;
                return _list.Count;
122 }
            }
123 }
        }
124
125 public void CopyTo(Array array, int index)
        public void CopyTo(Array array, int index)
126
 
         {
{
127 _list.CopyTo(array,index);
            _list.CopyTo(array,index);
128 }
        }
129
130 public object SyncRoot
        public object SyncRoot
131
 
         {
{
132 get
            get
133
 
             {
{
134 return _list.SyncRoot;
                return _list.SyncRoot;
135 }
            }
136 }
        }
137
138 #endregion
        #endregion
139
140
 IEnumerable 成员#region IEnumerable 成员
        IEnumerable 成员#region IEnumerable 成员
141
142 public IEnumerator GetEnumerator()
        public IEnumerator GetEnumerator()
143
 
         {
{
144 return _list.GetEnumerator();
            return _list.GetEnumerator();
145 }
        }
146
147 #endregion
        #endregion
148 }
    }
149 }
}
150 客户代码:
客户代码:

 客户代码
客户代码
1 private void button9_Click(object sender, System.EventArgs e)
    private void button9_Click(object sender, System.EventArgs e)
2
 
         {
{
3 Relaction.Collections.MyStrongTypeList list = new Relaction.Collections.MyStrongTypeList();
            Relaction.Collections.MyStrongTypeList list = new Relaction.Collections.MyStrongTypeList();
4 list.Add("1");
            list.Add("1");
5 list.Add("2");
            list.Add("2");
6 list.Add("nantest");
            list.Add("nantest");
7 for(int i =0;i<list.Count;i++)
            for(int i =0;i<list.Count;i++)
8
 
             {
{
9 label1.Text += list[i].ToString();
                label1.Text += list[i].ToString();
10 }
            }
11 }
        }

 自定义强类型集合类
自定义强类型集合类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 MyStrongTypeList:IList
    public class MyStrongTypeList:IList10

 
     {
{11
 private ArrayList _list;
        private ArrayList _list;12
 public MyStrongTypeList()
        public MyStrongTypeList()13

 
         {
{14
 _list = new ArrayList();
            _list = new ArrayList();15
 }
        }16

 IList 成员#region IList 成员
        IList 成员#region IList 成员17

18
 public bool IsReadOnly
        public bool IsReadOnly19

 
         {
{20
 get
            get21

 
             {
{22
 return _list.IsReadOnly;
                return _list.IsReadOnly;23
 }
            }24
 }
        }25

26
 public object this[int index]
        public object this[int index]27

 
         {
{28
 get
            get29

 
             {
{30
 return _list[index];
                return _list[index];31
 }
            }32
 set
            set33

 
             {
{34
 _list[index] = value;
                _list[index] = value;35
 }
            }36
 }
        }37

38
 public void RemoveAt(int index)
        public void RemoveAt(int index)39

 
         {
{40
 _list.RemoveAt(index);
            _list.RemoveAt(index);41
 }
        }42

43
 void IList.Insert(int index, object value)
        void IList.Insert(int index, object value)44

 
         {
{45
 _list.Insert(index,value);
            _list.Insert(index,value);46
 }
        }47

48
 public void Insert(int index,string value)
        public void Insert(int index,string value)49

 
         {
{50
 ((IList)this).Insert(index,value);
            ((IList)this).Insert(index,value);51
 }
        }52

53
 void IList.Remove(object value)
        void IList.Remove(object value)54

 
         {
{55
 _list.Remove(value);
            _list.Remove(value);56
 }
        }57
 public void Remove(string value)
        public void Remove(string value)58

 
         {
{59
 ((IList)this).Remove(value);
            ((IList)this).Remove(value);60
 }
        }61

62
 bool IList.Contains(object value)
        bool IList.Contains(object value)63

 
         {
{64
 return _list.Contains(value);
            return _list.Contains(value);65
 }
        }66

67
 public bool Contains(string value)
        public bool Contains(string value)68

 
         {
{69
 return ((IList)this).Contains(value);
            return ((IList)this).Contains(value);70
 }
        }71

72
 public void Clear()
        public void Clear()73

 
         {
{74
 _list.Clear();
            _list.Clear();75
 }
        }76

77
 int IList.IndexOf(object value)
        int IList.IndexOf(object value)78

 
         {
{79
 return _list.IndexOf(value);
            return _list.IndexOf(value);80
 }
        }81

82
 public int IndexOf(string value)
        public int IndexOf(string value)83

 
         {
{84
 return ((IList)this).IndexOf(value);
            return ((IList)this).IndexOf(value);85
 }
        }86

87
 int IList.Add(object value)
        int IList.Add(object value)88

 
         {
{89
 return _list.Add(value);
            return _list.Add(value);90
 }
        }91

92
 public int Add(string value)
        public int Add(string value)93

 
         {
{94
 return ((IList)this).Add(value);
            return ((IList)this).Add(value);95
 }
        }96

97
 public bool IsFixedSize
        public bool IsFixedSize98

 
         {
{99
 get
            get100

 
             {
{101
 return _list.IsFixedSize;
                return _list.IsFixedSize;102
 }
            }103
 }
        }104

105
 #endregion
        #endregion106

107

 ICollection 成员#region ICollection 成员
        ICollection 成员#region ICollection 成员108

109
 public bool IsSynchronized
        public bool IsSynchronized110

 
         {
{111
 get
            get112

 
             {
{113
 return _list.IsSynchronized;
                return _list.IsSynchronized;114
 }
            }115
 }
        }116

117
 public int Count
        public int Count118

 
         {
{119
 get
            get120

 
             {
{121
 return _list.Count;
                return _list.Count;122
 }
            }123
 }
        }124

125
 public void CopyTo(Array array, int index)
        public void CopyTo(Array array, int index)126

 
         {
{127
 _list.CopyTo(array,index);
            _list.CopyTo(array,index);128
 }
        }129

130
 public object SyncRoot
        public object SyncRoot131

 
         {
{132
 get
            get133

 
             {
{134
 return _list.SyncRoot;
                return _list.SyncRoot;135
 }
            }136
 }
        }137

138
 #endregion
        #endregion139

140

 IEnumerable 成员#region IEnumerable 成员
        IEnumerable 成员#region IEnumerable 成员141

142
 public IEnumerator GetEnumerator()
        public IEnumerator GetEnumerator()143

 
         {
{144
 return _list.GetEnumerator();
            return _list.GetEnumerator();145
 }
        }146

147
 #endregion
        #endregion148
 }
    }149
 }
}150


 客户代码
客户代码1
 private void button9_Click(object sender, System.EventArgs e)
    private void button9_Click(object sender, System.EventArgs e)2

 
         {
{3
 Relaction.Collections.MyStrongTypeList list = new Relaction.Collections.MyStrongTypeList();
            Relaction.Collections.MyStrongTypeList list = new Relaction.Collections.MyStrongTypeList();4
 list.Add("1");
            list.Add("1");5
 list.Add("2");
            list.Add("2");6
 list.Add("nantest");
            list.Add("nantest");7
 for(int i =0;i<list.Count;i++)
            for(int i =0;i<list.Count;i++)8

 
             {
{9
 label1.Text += list[i].ToString();
                label1.Text += list[i].ToString();10
 }
            }11
 }
        }.jpg) 
  
 
                    
                     
                    
                 
                    
                
 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号