实现Ilist接口的类
类:

功能齐全的集合
1
using System;
2
using System.Collections;
3
4
namespace Relaction.Collections
5

{
6
/**//// <summary>
7
/// MyList 的摘要说明。
8
/// </summary>
9
public class MyList:IList
10
{
11
private ArrayList _list;
12
public MyList()
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
public void Insert(int index, object value)
44
{
45
_list.Insert(index,value);
46
}
47
48
public void Remove(object value)
49
{
50
_list.Remove(value);
51
}
52
53
public bool Contains(object value)
54
{
55
return _list.Contains(value);
56
}
57
58
public void Clear()
59
{
60
_list.Clear();
61
}
62
63
public int IndexOf(object value)
64
{
65
return _list.IndexOf(value);
66
}
67
68
public int Add(object value)
69
{
70
return _list.Add(value);
71
}
72
73
public bool IsFixedSize
74
{
75
get
76
{
77
return _list.IsFixedSize;
78
}
79
}
80
81
#endregion
82
83
ICollection 成员#region ICollection 成员
84
85
public bool IsSynchronized
86
{
87
get
88
{
89
return _list.IsSynchronized;
90
}
91
}
92
93
public int Count
94
{
95
get
96
{
97
return _list.Count;
98
}
99
}
100
101
public void CopyTo(Array array, int index)
102
{
103
_list.CopyTo(array,index);
104
}
105
106
public object SyncRoot
107
{
108
get
109
{
110
return _list.SyncRoot;
111
}
112
}
113
114
#endregion
115
116
IEnumerable 成员#region IEnumerable 成员
117
118
public IEnumerator GetEnumerator()
119
{
120
return _list.GetEnumerator();
121
}
122
123
#endregion
124
}
125
}
客户:

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

4
namespace Relaction.Collections5


{6

/**//// <summary>7
/// MyList 的摘要说明。8
/// </summary>9
public class MyList:IList10

{11
private ArrayList _list;12
public MyList()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
public void Insert(int index, object value)44

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

48
public void Remove(object value)49

{50
_list.Remove(value);51
}52

53
public bool Contains(object value)54

{55
return _list.Contains(value);56
}57

58
public void Clear()59

{60
_list.Clear();61
}62

63
public int IndexOf(object value)64

{65
return _list.IndexOf(value);66
}67

68
public int Add(object value)69

{70
return _list.Add(value);71
}72

73
public bool IsFixedSize74

{75
get76

{77
return _list.IsFixedSize;78
}79
}80

81
#endregion82

83

ICollection 成员#region ICollection 成员84

85
public bool IsSynchronized86

{87
get88

{89
return _list.IsSynchronized;90
}91
}92

93
public int Count94

{95
get96

{97
return _list.Count;98
}99
}100

101
public void CopyTo(Array array, int index)102

{103
_list.CopyTo(array,index);104
}105

106
public object SyncRoot107

{108
get109

{110
return _list.SyncRoot;111
}112
}113

114
#endregion115

116

IEnumerable 成员#region IEnumerable 成员117

118
public IEnumerator GetEnumerator()119

{120
return _list.GetEnumerator();121
}122

123
#endregion124
}125
}客户:
1
private void button8_Click(object sender, System.EventArgs e)2

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

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

浙公网安备 33010602011771号