Strong-Type Collection
自定义集合类:

自定义强类型集合类
1
using System;
2
using System.Collections;
3
4
namespace Relaction.Collections
5

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

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

4
namespace Relaction.Collections5


{6

/**//// <summary>7
/// 8
/// </summary>9
public class MyStrongTypeList:IList10

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

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

IList 成员#region IList 成员17

18
public bool IsReadOnly19

{20
get21

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

26
public object this[int index]27

{28
get29

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

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

38
public void RemoveAt(int index)39

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

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

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

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

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

53
void IList.Remove(object value)54

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

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

62
bool IList.Contains(object value)63

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

67
public bool Contains(string value)68

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

72
public void Clear()73

{74
_list.Clear();75
}76

77
int IList.IndexOf(object value)78

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

82
public int IndexOf(string value)83

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

87
int IList.Add(object value)88

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

92
public int Add(string value)93

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

97
public bool IsFixedSize98

{99
get100

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

105
#endregion106

107

ICollection 成员#region ICollection 成员108

109
public bool IsSynchronized110

{111
get112

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

117
public int Count118

{119
get120

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

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

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

130
public object SyncRoot131

{132
get133

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

138
#endregion139

140

IEnumerable 成员#region IEnumerable 成员141

142
public IEnumerator GetEnumerator()143

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

147
#endregion148
}149
}150

1
private void button9_Click(object sender, System.EventArgs e)2

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

{9
label1.Text += list[i].ToString();10
}11
}

浙公网安备 33010602011771号