7. 集合类应该实现泛型接口
原文引用:
** Collections should implement generic interface
**
|
TypeName:
|
CollectionsShouldImplementGenericInterface
---|---
CheckId:
|
CA1010
Category:
|
Microsoft.Design
Message Level:
|
Error
Certainty:
|
90%
Breaking Change:
|
NonBreaking
Cause: An externally visible type implements the System.Collections.IEnumerable interface but does not implement the System.Collections.Generic.IEnumerable
1<t> interface, and the containing assembly targets .NET Framework version 2.0. This rule ignores types that implement System.Collections.IDictionary .
2
3** Rule Description
4
5**
6
7To broaden the usability of a collection, implement one of the generic collection interfaces. Then the collection can be used to populate generic collection types such as the following:
8
9 * System.Collections.Generic.List<t>
10
11 * System.Collections.Generic.Queue<t>
12
13 * System.Collections.Generic.Stack<t>
14
15
16
17
18** How to Fix Violations
19
20**
21
22To fix a violation of this rule, implement one of the following generic collection interfaces:
23
24 * System.Collections.Generic.IEnumerable<t>
25
26 * System.Collections.Generic.ICollection<t>
27
28 * System.Collections.Generic.IList<t>
29
30
31
32
33** When to Exclude Messages
34
35**
36
37It is safe to exclude a message from this rule; however, the collection will have a more limited use.
38
39** Example Code
40
41**
42
43The following example shows a collection, IntegerCollection , that violates the rule. In GenericIntegerCollection , the collection is modified to satisfy the rule by implementing System.Collections.Generic.IEnumerable<t> . Finally, the collection is used to populate a strongly typed stack.
44
45[C#]
46
47using System;
48
49using System.Collections;
50
51using System.Collections.Generic;
52
53namespace DesignLibrary
54
55{
56
57// This class violates the rule.
58
59public class IntegerCollection: CollectionBase
60
61{
62
63// Methods overrides using Int32.
64
65}
66
67// This class satisfies the rule.
68
69public class GenericIntegerCollection:
70
71CollectionBase, IEnumerable<int>
72
73{
74
75public int Add(int value)
76
77{
78
79return InnerList.Add(value);
80
81}
82
83// Other method overrides using Int32.
84
85public new IEnumerator<int> GetEnumerator()
86
87{
88
89foreach (int data in InnerList)
90
91{
92
93yield return data;
94
95}
96
97}
98
99}
100
101class Test
102
103{
104
105static void Main ()
106
107{
108
109IntegerCollection intCollection = new IntegerCollection();
110
111// The following line would generate a compiler error.
112
113//Stack<int> integerStack = new Stack<int>(intCollection);
114
115GenericIntegerCollection genericIntCollection =
116
117new GenericIntegerCollection();
118
119genericIntCollection.Add(2);
120
121genericIntCollection.Add(4);
122
123Stack<int> integerStack = new Stack<int>(genericIntCollection);
124
125Console.WriteLine(integerStack.Pop());
126
127Console.WriteLine(integerStack.Pop());
128
129}
130
131}
132
133}
134
135** Related Rules
136
137**
138
139Avoid excessive parameters on generic types
140
141Do not declare static members on generic types
142
143Do not expose generic lists
144
145Do not nest generic types in member signatures
146
147Generic methods should provide type parameter
148
149Use generic event handler instances
150
151Use generics where appropriate
152
153** See Also
154
155**
156
157Generics DesignGuidelines
158
159### 引起的原因:
160
161一个使用 .NET Framework2.0 的程序集有一个输出类型实现了 System.Collections.IEnumerable 接口,但是没有实现 System.Collections.Genceric.IEnumerable<t> 接口。如果这个类型实现了 System.Collections.IDictionary 接口,将不被这个规则所检察。
162
163### 描述:
164
165为了提升一个集合的可用性,应该实现范型集合接口中的一个。这样,这个集合将能够被用来声明下面的范型类。
166
167 * System.Collections.Generic.List<t>
168
169 * System.Collections.Generic.Queue<t>
170
171 * System.Collections.Generic.Stack<t>
172
173
174
175
176### 修复:
177
178实现下面的范型集合接口中的一个。
179
180 * System.Collections.Generic.IEnumerable<t>
181
182 * System.Collections.Generic.ICollection<t>
183
184 * System.Collections.Generic.IList<t>
185
186
187
188
189### 例外:
190
191不符合这条规则是安全的,但是,这样将会使你的集合类有更多的使用限制。
192
193### 译注:
194
195实现至少一个泛型集合接口使你的类型能够被用来构造下面的范型集合类。
196
197 * System.Collections.Generic.List<t>
198
199 * System.Collections.Generic.Queue<t>
200
201 * System.Collections.Generic.Stack<t>
202
203
204
205
206例如在原文的例子中的 GenericIntegerCollection 对象就可以被用来构造一个 Stack<int> 对象,但是如果你不实现这些接口,构造 Stack<int> 对象将会产生一个编译警告。实现范型集合接口将会使你的使用者拥有更多的灵活性去使用你的集合类。</int></int></t></t></t></t></t></t></t></t></t></t></int></int></int></int></int></int></t></t></t></t></t></t></t></t>