Commonly used collection types
Collection types represent different ways to collect data, such as hash tables, queues, stacks, bags, dictionaries, and lists.
In collections based on the IDictionary interface, every element contains both a key and a value. These types include:
The KeyedCollection<TKey,TItem> class is unique because it is a list of values with keys embedded within the values. As a result, it behaves both like a list and like a dictionary.
When you need efficient multi-threaded collection access, use the generic collections in the System.Collections.Concurrent namespace.
Generic collections are the best solution to strong typing. For example, adding an element of any type other than an Int32 to a
List<Int32> collection causes a compile-time error. However, if your language does not support generics, the System.Collections namespace includes abstract base classes that you can extend to create collection classes that are strongly typed. These base classes include:
How collections vary
Collections vary in how they store, sort, and compare elements, and how they perform searches.
All collections use zero-based indexes except Array, which allows arrays that are not zero-based.
You can access the elements of a SortedList or a KeyedCollection<TKey,TItem> by either the key or the element's index. You can only access the elements of a Hashtable or a Dictionary<TKey,TValue> by the element's key.
Use LINQ with collection types
The LINQ to Objects feature provides a common pattern for accessing in-memory objects of any type that implements IEnumerable or IEnumerable<T>. LINQ queries have several benefits over standard constructs like
- They are concise and easier to understand.
- They can filter, order, and group data.
- They can improve performance.
|Collections and Data Structures||Discusses the various collection types available in .NET, including stacks, queues, lists, arrays, and dictionaries.|
|Hashtable and Dictionary Collection Types||Describes the features of generic and nongeneric hash-based dictionary types.|
|Sorted Collection Types||Describes classes that provide sorting functionality for lists and sets.|
|Generics||Describes the generics feature, including the generic collections, delegates, and interfaces provided by .NET. Provides links to feature documentation for C#, Visual Basic, and Visual C++, and to supporting technologies such as reflection.|