Collections in C#

This article describes the benefits and types of collections in C#.

Introduction

This article explains collections and their various types with their benefits. We know that a collection is a group of objects and these groups of objects are handled by our programmers. Some of them are ArrayList, HashTable, Stack and Queue. All these collections are designed around a set of defined Interface. Several built-in implementations of these interface, such as ArrayList, HashTable and Queue are provided, that we can use as it is. We can also implement our own collection. The .NET Framework supports the following five types of collections in C#:

  • Non-Generic
  • Specialized
  • Bit-Based
  • Generic, and
  • Concurrent

Collection

Non-Generic Collections

The non-generic collection implements several fundamental data structures, including a dynamic array, stack and queue. They also include dictionaries, in which we can store key/value pairs. This collection operates on data of type object and it can be used to store any type of data, because it stores a reference. These collections are not type safe. The non-generic collection classes and interface are in system.Collections.

Specialized Collections

The specialized collections operate on a specific type of data or operate in a unique way. Like specialized collections for strings. There are also specialized collections that use a single link list. The specialized collection are declared in system.Collections.Specialized.

 Bit-based Collections

The collection API defines one bit-based collection called BitArray. BitArray supports bitwise operations on bits, such as AND and XOR. BitArray is declared in system.Collections.

Generic Collections

Generic collections provide us a generic implementation of several standard data structures, such as linked list, stack, queue and dictionary. These  collections are generic, they are type-safe. This means that only items are type-compatible with the type of collection can be stored in a generic collection. Generic collections are declared in system.Collections.Generic.

Concurrent Collection

The concurrent collections supports multi-threaded access to a collection. This collection is defined in system.Collections.Concurrent.

Note: There are also several classes in the system.Collections.ObjectModel namespace that supports programmers creating their own generic collections.

All collections support an enumerator including the non-generic interfaces IEnumerator and IEnumerable and the generic interface IEnumerator<T> and IEnumerable<T>. An enumerator provides a standardized way of accessing the element within a collection, one at a time. Because each collection must implement either a generic or non-generic from IEnumerable, the elements of any collection class can be accessed through the method defined by IEnumerator or IEnumerator<T>.

Non-Generic Collections

We have seen the definition and some details about non-generic collections. Now we will see a non-generic interface defined by a set of interfaces and the classes that implement those interfaces. Each of them are described in the following table.

  • Non-Generic Interfaces
     

    Interface

    Description

    ICollection Defines the elements that all non-generic collections must have
    IComparer Defines the Compare() method that does a comparison on objects stored in a collection
    IDictionary Defines a collection that consists of key/value pairs
    IDictionaryEnumerator Defines the enumerator for a collection that implements IDictionary
    IEnumerable Defines the GetEnumerator() method, that supplies the enumerator for a collection class
    IEnumerator Provides methods that enable the content of a collection to be obtained one at a time.
    IEqualityComparer Compares two objects for equality
    IHashCodeProvider Declared obsolete. Use IEqualityComparer instead.
    IList Defines a collection that can be accessed via an indexer
    IStruturalComparable Defines the compareTo() method that is used for structural comparisons
    IStructuralEquatable Defines the equal() method that is used to determine structural equality. It also defines the GetHashCode() method
  • Non-Generic Classes
     

    Class

    Description

    ArrayList This is a dynamic Array. This array gets resize( grow, shrink)
    Hashtable A hash table for key/ value pairs
    Queue First-in-first-out list
    SortedList A sorted list of key/value pair
    Stack First-in,last-out list

Bit-based Collections

The BitArray class supports a collection of bits. It stores bits rather then objects, BitArray is different from other collections and supports the basic collection implementing ICollection and IEnumerable. It also implemnts ICloneable.

Specialized Collections

This collection is helpful in the optimization of our work.

Specialized Collection

Description

CollectionUtil Conatains a factory method that creates a collection that stores strings, but ignores case differences
HybridDictionary A collection that uses a ListDictionary to store key/value pairs when there are a few elements in the collection. When the collection grows beyond a certain size, a HashTable is automatically used to store elements
ListDictionary A collection that stores key/value pairs in a linked list. This is recommend only for small collections
NamedValueCollection A sorted collection of key/value pairs in which both the key and value are of type string
OrderdDictionary A collection of key/value pairs that can be indexed
StringCollection A collection optimized for storing a string
StringDictionary A hash table of key/value pairs in which both the key and the value are of type string

Generic Collections

Generic collections are defined by a set of interfaces and the classes that implement those interfaces.

  • Generic Interface
     

    Generic Interface

    Description

    ICollection<T> Defines the functional features for the generic collections
    IComparer<T> Defines the generic Compare() method that does a comparison on objects stored in a collection
    IDictionary<Tkey,Tvalue> Defines a generic collection that consists of key/value pairs
    IEnumerable<T> Defines the generic GetEnumerator() method, that supplies the enumerator for a collection class
    IEnumerator<T> Provides methods that enable the content of a collection to be obtained one at a time
    IEqualityComparer<T> Compares two objects for equality
    IList<T> Defines a generic collection that can be accessed via an indexer
    ISet<T> Defines a generic collection that represents a set
  • Generic Class
     

    Generic Class

    Description

    Dictionary<Tkey,Tvalue> Stores key/value pairs, the same as a non-generic HashTable class
    HashSet<T> Stores a set of unique vlaues using a hash table
    LinkedList<T> Stores elements in a doubly linked list
    List<T> Dynamic array, the same as the non-generic ArrayList class
    Queue<T> A first-in, first-out list, the same as the non-generic Queue class
    SortedDictionary<Tkey,TValue> Sorted list of key/value pairs
    SortedList<TKey,TValue> A sorted list of key/value pairs, this is also the same as the non generic SortedList class
    SortedSet<T> A sorted set
    Stack<T> A first-in, last-out list, It is the same as the non-generic stack class

Concurrent Collections

This was added in .NET framework 4.0 and have a new namespace called system.Collection.Concurrent. It contains collections that are thread-safe and designed to be used for parallel programming, It means that it is safe to use in a multi-threaded program in which two or more concurrently executing threads might access a collection simultaneously.  

Concurrent Collection

Description

BlockingCollection<T> Provides a wrapper for blocking implementation of the IProducerConsumerCollection<T> interface
ConcurrentBag<T> An unordered implementation of the IProducerConsumerCollection<T> interface that works best when a single thread produces and consume the information
ConcurrentDictionary<TKey,TValue> Store a key/value pair, it implements a concurrent dictionary
ConcurrentQueue<T> Implements a concurrent queue, also implements IProducerConsumerCollection<T>
ConcurrentStack<T> Implements a concurrent stack. Implements IProducerConsumerCollection<T>

Summary

In this article we have covered various types of collections in C#. We have also covered the various types of classes and interface used in collections, with their descriptions.