My two cents on collections in C#

Leverage collections in .Net to store to promote type safety, facilitate code reuse, and improve performance when working with in-memory collection of objects

Collections in C#

Collections in C#


Collections are used for storage and retrieval of data. You use collections in your application to allocate memory dynamically to store elements and then retrieve them using key or index as and when needed. Basically a collection represents a set of objects that you can access by iterating each of the elements of the collection.

Microsoft .Net framework provides excellent support for working with collections. You can take advantage of the types contained in the System.Collections, System.Collections.Generic, System.Collections.Concurrent, System.Collections.Immutable namespaces to work with collections.

Generic and non–generic collections

There are two types of collections in .Net: generic collections and non-generic collections. Generic collections are those that provide type safety and hence better performance as you don't have the boxing and un-boxing overheads when storing and retrieving elements to and from generic collections. You can leverage generics to enforce type safety and improve performance when working with an in-memory collection of data as there is no boxing and unboxing overhead involved. When you use generics, the compiler makes compile time checks on your code for conformance to type safety. Note that type safe collections or strongly collections are those that contain elements of a pre-defined or known type.

While the standard collections are contained in the System.Collections namespace, the generic collections are contained in the System.Collections.Generic namespace. Note that you can leverage generics to create type safe collections for improved performance when working with collections. Some of the important classes in the System.Collections.Generic namsepace include: List<T>, Queue<T>, HashSet<T>, LinkedList<T>, Stack<T>, LinkedListNode<T> and Dictionary<TKey, TValue>.

Types inside the System.Collections namespace

When working with collections you would need to take advantage of the System.Collections namespace. This contains a collection of classes, interfaces, methods and properties that you can make use of to work with different collections.

The types in the System.Collections namespace store data as objects of type Object. The following is the list of some of the important interfaces that are contained in the System.Collections namespace.

  1. IEnumerable -- this interface exposes an IEnumerator that can used to iterate a non-generic collection using a foreach loop
  2. IEnumerator -- this is used to iterate a non-generic collection
  3. ICollection -- this interface extends the IEnumerable interface and defines enumerators and synchronization methods for non-generic collections
  4. IList -- this interface represents a non-generic ordered collection that can be accessed using index
  5. IDictionary -- this interface is used to represent a non-generic collection of objects that are organized as key / value pairs

And, here's the list of the important classes in the System.Collections namespace.

  1. ArrayList -- this implements the IList interface and represents a collection whole size can increase dynamically and can store objects of any type. Data inside an ArrayList instance can be accessed using the index.
  2. Hashtable -- this represents a collection that can store objects as key / value pairs
  3. BitArray -- this represents a collection that can manage a compact array of bit values where the values are represented as Booleans
  4. Queue -- this represents a first –in first – out non – generic collection of objects
  5. Stack -- this represents a last –in last – out non – generic collection of objects
  6. SortedList -- this represents a collection of objects stored as key / value pairs that are sorted by the keys. Objects in a sorted list are accessible both using their keys and indexes.

Note that while the ICollection interface extends the IEnumerable interface, both the IDictionary and IList interfaces extend the ICollection interface. You also have the DictionaryEntry structure in this namespace. You can use the DictionaryEntry structure for iterating through Hashtable instances. On the contrary, the KeyValuePair structure can be used to iterate items in a Dictionary (a Dictionary is also known as associative array) instance.

The System.Collections.Concurrent namespace

With .Net Framework 4 the collection types in the System.Collections.Concurrent namespace provide an excellent support for thread safety while accessing data from the collection type instances using multiple threads concurrently. Synchronization is a process that is used to prevent multiple threads from accessing a shared resource concurrently -- you can take advantage of thread synchronization to restrict access to a shared resource.  Some of the important thread-safe collection classes in the System.Collections. Generic namespace include: BlockingCollection<T>, ConcurrentQueue<T>, ConcurrentStack<T> and ConcurrentDictionary<TKey, TValue>.

This article is published as part of the IDG Contributor Network. Want to Join?

From CIO: 8 Free Online Courses to Grow Your Tech Skills
View Comments
Join the discussion
Be the first to comment on this article. Our Commenting Policies