However, instead of using List(Of T), consider
Collection(Of T). List(Of T) doesn??™t implement the protected overridable methods that
Collection(Of T) implements. Therefore, if you derive your list type from List(Of T), your
derived type has no way to respond when you make modifications to the list. On the other
hand, List(Of T) serves as a great tool to use when you need to embed a raw list implementation
within a custom collection, since it is devoid of overridable method calls, such as
Collection(Of T), and is more efficient as a result.
Another useful type within the System.Collections.ObjectModel namespace is the
ReadOnlyCollection(Of T) type, which is a wrapper you can use to implement read-only
collections. The constructor for ReadOnlyCollection(Of T) accepts an IList(Of T) parameter
type. Thus you can use a ReadOnlyCollection(Of T) to wrap any type that implements
IList(Of T), including Collection(Of T). Naturally, if users access the ICollection(Of T).
IsReadOnly property, the answer will be True. Any time users call a modifying method, such
as ICollection(Of T).Clear(), an exception of type NotSupportedException will be thrown.
Moreover, in order to call modifying methods, you must cast the ReadOnlyCollection(Of T)
reference to the interface containing the method, since ReadOnlyCollection(Of T) implements
all modifying methods explicitly.
Pages:
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352