This documentation is archived and is not being maintained.

Collection<T> Class

Provides the base class for a generic collection.

Namespace:  System.Collections.ObjectModel
Assembly:  mscorlib (in mscorlib.dll)

generic<typename T>
public ref class Collection : IList<T>, 
	ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable

Type Parameters


The type of elements in the collection.

The Collection<T> 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<T> class itself.

The Collection<T> 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<T> objects can be modified. However, a Collection<T> object that is initialized with a read-only IList<T> object cannot be modified. See ReadOnlyCollection<T> 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<T> accepts nullptr 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<T> class. The second example shows how to derive a collection class from a constructed type of Collection<T>, and how to override the protected methods of Collection<T> to provide custom behavior.

Example 1

The following code example demonstrates many of the properties and methods of Collection<T>. 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.

No code example is currently available or this language may not be supported.

Example 2

The following code example shows how to derive a collection class from a constructed type of the Collection<T> 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<T> to demonstrate the custom event.

No code example is currently available or this language may not be supported.

    System.Collections.ObjectModel::KeyedCollection<TKey, TItem>

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<T> 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 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0