Most useful types of Collections

Many common collections are provided by the .NET Framework. Each type of collection is designed for a specific purpose.

Some of the common collection classes are described in this section:

  • System.Collections.Generic classes

A generic collection is useful when every item in the collection has the same data type

The following table lists some of the frequently used classes of the System.Collections.Generic namespace:

ClassDescription
Dictionary<TKey,TValue>Represents a collection of key/value pairs that are organized based on the key.
List<T>Represents a list of objects that can be accessed by index. Provides methods to search, sort, and modify lists.
Queue<T>Represents a first in, first out (FIFO) collection of objects.
SortedList<TKey,TValue>Represents a collection of key/value pairs that are sorted by key based on the associated IComparer<T> implementation.
Stack<T>Represents a last in, first out (LIFO) collection of objects.
  • System.Collections.Concurrent classes

The classes in the System.Collections.Concurrent namespace should be used instead of the corresponding types in the System.Collections.Generic and System.Collections namespaces whenever multiple threads are accessing the collection concurrently

Some classes included in the System.Collections.Concurrent namespace are BlockingCollection<T>, ConcurrentDictionary<TKey,TValue>, ConcurrentQueue<T>, and ConcurrentStack<T>.

  • System.Collections classes

The classes in the System.Collections namespace do not store elements as specifically typed objects, but as objects of type Object.

The following table lists some of the frequently used classes in the System.Collections namespace:

ClassDescription
ArrayListRepresents an array of objects whose size is dynamically increased as required.
HashtableRepresents a collection of key/value pairs that are organized based on the hash code of the key.
QueueRepresents a first in, first out (FIFO) collection of objects.
StackRepresents a last in, first out (LIFO) collection of objects.
  • Visual Basic Collection class

You can use the Visual Basic Collection class to access a collection item by using either a numeric index or a String key. You can add items to a collection object either with or without specifying a key. If you add an item without a key, you must use its numeric index to access it.

The Visual Basic Collection class stores all its elements as type Object, so you can add an item of any data type. There is no safeguard against inappropriate data types being added.

Leave a comment

Design a site like this with WordPress.com
Get started