Commonly Used Collection Types

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Collection types are the common variations of data collections, such as hash tables, queues, stacks, dictionaries, and lists.

Collections are based on the ICollection interface, the IList interface, the IDictionary interface, or their generic counterparts. The IList interface and the IDictionary interface are both derived from the ICollection interface; therefore, all collections are based on the ICollection interface either directly or indirectly.

Every element contains a value in collections based on the IList interface (such as Array, or List<T>) or based directly on the ICollection interface LinkedList<T>).

Every collection based on the ICollection interface (such as the Dictionary<TKey, TValue> generic class) contains both a key and a value.

The KeyedCollection<TKey, TItem> class is unique because it is a list of values with keys embedded within the values and, therefore, it behaves like a list and like a dictionary.

Collections can vary, depending on how the elements are stored, how they are sorted, how searches are performed, and how comparisons are made. The elements of a Dictionary<TKey, TValue> are accessible only by the key of the element, but the elements of a KeyedCollection<TKey, TItem> are accessible either by the key or by the index of the element. The indexes in all collections are zero-based, except Array, which allows arrays that are not zero-based.

Array Collection Type

The Array class is not part of the System.Collections namespaces. However, it is still a collection because it is based on the IList interface.

The rank of an Array object is the number of dimensions in the Array. An Array can have one or more ranks.

Unlike the classes in the System.Collections namespaces, Array has a fixed capacity. To increase the capacity, you must create a new Array object with the required capacity, copy the elements from the old Array object to the new one, and delete the old Array.

List Collection Types

The generic List<T> class provides features that are offered in most System.Collections classes but are not in the Array class. For example:

  • The capacity of an Array is fixed, whereas the capacity of a List<T> is automatically expanded as required. If the value of the Capacity property changes, the memory reallocation and copying of elements occur automatically.

  • The List<T> class provide methods that add, insert, or remove a range of elements. In Array, you can get or set the value of only one element at a time.

  • The List<T> provide methods that return read-only and fixed-size wrappers to the collection. The Array class does not.

On the other hand, Array offers some flexibility that List<T> does not. For example:

  • You can set the lower bound of an Array, but the lower bound of a List<T> is always zero.

  • An Array can have multiple dimensions, while a List<T> always has exactly one dimension.

Most situations that call for an array can use a List<T> instead; they are easier to use and, in general, have performance similar to an array of the same type.

Array is in the System namespace; List<T> is in the System.Collections.Generic namespace.

Dictionary Collection Types

You can use the Dictionary<TKey, TValue> generic class which implements the IDictionary interface. The Dictionary<TKey, TValue> generic class also implements the IDictionary<TKey, TValue> generic interface. Therefore, each element in these collections is a key-and-value pair.

Queue Collection Types

The Queue<T> generic class is a first-in-first-out collection class that implements the ICollection interface and the ICollection<T> generic interface.

The Queue<T> and Stack<T> generic classes are useful when you need temporary storage for information, that is, when you might want to discard an element after retrieving its value. Use Queue<T> if you need to access the information in the same order that it is stored in the collection. Use Stack<T> if you need to access the information in reverse order.

Three main operations can be performed on a Queue<T> and its elements:

  • The Enqueue method adds an element to the end of the queue.

  • The Dequeue method removes the oldest element from the start of the queue.

  • The Peek method returns the oldest element from the start of the queue but does not remove it from the queue.

Stack Collection Types

The Stack<T> generic class is a last-in-first-out collection class that implements the ICollection interface. The Stack<T> generic class also implements the ICollection<T> generic interface.

Use a queue if you need to access the information in the same order that it is stored in the collection. Use a stack f you need to access the information in reverse order.

A common use for a stack is preserving variable states during calls to other procedures.

Three main operations can be performed on a stack and its elements:

  • The Push method inserts an element at the top of the stack.

  • The Pop method removes an element at the top of the stack.

  • The Peek method returns an element at the top of the stack but does not remove it from the stack.

Reference

  • System.Collections
    Provides reference documentation for the System.Collections namespace, which contains interfaces and classes that define various collections of objects.

  • System.Collections.Generic
    Provides reference documentation for the System.Collections.Generic namespace, which contains interfaces and classes that define generic collections.

  • ICollection
    Describes the major features of the ICollection class, which defines size, enumerators and synchronization methods for all nongeneric collections.

  • ICollection<T>
    Describes the major features of the ICollection<T> class, which defines methods to manipulate generic collections.

  • IList
    Describes the major features of the IList class, which represents a nongeneric collection of objects that can be individually accessed by index.

  • IList<T>
    Describes the major features of the IList<T> class, which represents a collection of objects that can be individually accessed by index.

  • IDictionary
    Describes the major features of the IDictionary class, which represents a nongeneric collection of key/value pairs.

  • IDictionary<TKey, TValue>
    Describes the major features of the IDictionary<TKey, TValue> class, which represents a generic collection of key/value pairs.

See Also

Concepts

Other Resources