Collection Generic Class
Assembly: mscorlib (in mscorlib.dll)
[SerializableAttribute] [ComVisibleAttribute(false)] generic<typename T> public ref class Collection : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
The type of elements in the collection.
The Collection class can be used immediately by creating an instance of one of its constructed types; all you have to do is specify the type of object to be contained in the collection. In addition, you can derive your own collection type from any constructed type, or derive a generic collection type from the Collection class itself.
The Collection class provides protected methods that can be used to customize its behavior when adding and removing items, clearing the collection, or setting the value of an existing item.
Most Collection objects can be modified. However, a Collection object that is initialized with a read-only IList object cannot be modified. See ReadOnlyCollection for a read-only version of this class.
Elements in this collection can be accessed using an integer index. Indexes in this collection are zero-based.
Collection accepts a null reference (Nothing in Visual Basic) as a valid value for reference types and allows duplicate elements.Notes to Implementers: This base class is provided to make it easier for implementers to create a custom collection. Implementers are encouraged to extend this base class instead of creating their own.
This section contains two code examples. The first example demonstrates several properties and methods of the Collection class. The second example shows how to derive a collection class from a constructed type of Collection, and how to override the protected methods of Collection to provide custom behavior.
The following code example demonstrates many of the properties and methods of Collection. The code example creates a collection of strings, uses the Add method to add several strings, displays the Count, and lists the strings. The example uses the IndexOf method to find the index of a string and the Contains method to determine whether a string is in the collection. The example inserts a string using the Insert method and retrieves and sets strings using the default Item property (the indexer in C#). The example removes strings by string identity using the Remove method and by index using the RemoveAt method. Finally, the Clear method is used to clear all strings from the collection.
The following code example shows how to derive a collection class from a constructed type of the Collection generic class, and how to override the protected InsertItem, RemoveItem, ClearItems, and SetItem methods to provide custom behavior for the Add, Insert, Remove, and Clear methods, and for setting the Item property.
The custom behavior provided by this example is a Changed notification event that is raised at the end of each of the protected methods. The Dinosaurs class inherits Collection<string> (Collection(Of String) in Visual Basic) and defines the Changed event, which uses a DinosaursChangedEventArgs class for the event information, and an enumeration to identify the kind of change.
The code example calls several properties and methods of Collection to demonstrate the custom event.
Public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
A Collection can support multiple readers concurrently, as long as the collection is not modified. Even so, enumerating through a collection is intrinsically not a thread-safe procedure. To guarantee thread safety during enumeration, you can lock the collection during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter EditionThe Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.