Udostępnij za pośrednictwem


Commonly Used Collection Types

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. In collections based on the IList interface (such as Array, ArrayList, or List<T>) or directly on the ICollection interface (such as Queue, Stack, or LinkedList<T>), every element contains only a value. In collections based on the IDictionary interface (such as the Hashtable and SortedList classes, or the Dictionary<TKey, TValue> and SortedList<TKey, TValue> generic classes), every element 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.

Generic collections are the best solution to strong typing. However, if your language does not support generics, the System.Collections namespace includes base collections, such as CollectionBase, ReadOnlyCollectionBase, and DictionaryBase, which are abstract base classes that can be extended to create collection classes that are strongly typed.

Collections can vary, depending on how the elements are stored, how they are sorted, how searches are performed, and how comparisons are made. The Queue class and the Queue<T> generic class provide first-in-first-out lists, while the Stack class and the Stack<T> generic class provide last-in-first-out lists. The SortedList class and the SortedList<TKey, TValue> generic class provide sorted versions of the Hashtable class and the Dictionary<TKey, TValue> generic class. The elements of a Hashtable or a Dictionary<TKey, TValue> are accessible only by the key of the element, but the elements of a SortedList or 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.

The LINQ to Objects feature allows you to use LINQ queries to access in-memory objects as long as the object type implements IEnumerable or IEnumerable<T>. LINQ queries provide a common pattern for accessing data; are typically more concise and readable than standard foreach loops; and provide filtering, ordering and grouping capabilities. LINQ queries can also improve performance. For more information, see LINQ to Objects.

In This Section

Reference

  • Collections and Data Structures
    Discusses the various collection types available in the .NET Framework, including stacks, queues, lists, arrays, and structs.

  • Generics in the .NET Framework
    Describes the generics feature, including the generic collections, delegates, and interfaces provided by the .NET Framework. Provides links to feature documentation for C#, Visual Basic, and Visual C++, and to supporting technologies such as reflection.