joydip_kanjilal
Contributor

My two cents on collections in C#

opinion
Jan 15, 20164 mins
Software Development

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 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, Queue, HashSet, LinkedList, Stack, LinkedListNode and Dictionary.

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, ConcurrentQueue, ConcurrentStack and ConcurrentDictionary.

joydip_kanjilal
Contributor

Joydip Kanjilal is a Microsoft Most Valuable Professional (MVP) in ASP.NET, as well as a speaker and the author of several books and articles. He received the prestigious MVP award for 2007, 2008, 2009, 2010, 2011, and 2012.

He has more than 20 years of experience in IT, with more than 16 years in Microsoft .Net and related technologies. He has been selected as MSDN Featured Developer of the Fortnight (MSDN) and as Community Credit Winner several times.

He is the author of eight books and more than 500 articles. Many of his articles have been featured at Microsoft’s Official Site on ASP.Net.

He was a speaker at the Spark IT 2010 event and at the Dr. Dobb’s Conference 2014 in Bangalore. He has also worked as a judge for the Jolt Awards at Dr. Dobb's Journal. He is a regular speaker at the SSWUG Virtual Conference, which is held twice each year.

More from this author