C# Collections - how to choose?
Tags: c, collections
Published: 2021-10-25 05:41:29

---
title: C# Collections - how to choose?
published: true
description: 
tags: C#, collections
cover_image: https://images.unsplash.com/photo-1488841714725-bb4c32d1ac94?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=830&q=80
---
######Cover image [source](https://unsplash.com/@clemono)

##Collections
C# collections have plenty of options to use. When it comes to simple scenarios and small data handling any of these would work fine or at least give you the desired results. Things start to get messy when you have complex scenarios like multi-threading, huge chunks of data, indexing on collections, searching etc.

The Microsoft [documentation](https://docs.microsoft.com/en-us/dotnet/standard/collections/) on collections is a decent place to get started. One of the key things to note from this documentation is:

_There are two main types of collections; generic collections and non-generic collections. Generic collections are type-safe at compile time. Because of this, generic collections typically offer better performance. Generic collections accept a type parameter when they are constructed and do not require that you cast to and from the Object type when you add or remove items from the collection._

This means that, in general, a generic collection should be used. The non generic collections will be stored as Object. This means that they will need casting when you are playing around with data. IMHO, I think this will play a very imp role when it comes to performance.

The MSDN link I added before has an excellent piece of advice that will help us in understanding which collection is to be chosen based on our requirement.

| Collection        										  | Generic Option          | Non-generic Option  | Thread-safe or immutable collection options 													     |
| ------------------------------------------------------------|:-----------------------:| -------------------:|-----------------------------------------------------------------------------------------------------:|
| Store items as key/value pairs for quick look-up by key     | `Dictionary<TKey,TValue>` | `Hashtable`           | `ConcurrentDictionary<TKey,TValue>`, `ReadOnlyDictionary<TKey,TValue>`, `ImmutableDictionary<TKey,TValue>` |
| Access items by index      | `List<T>`      |   `Array`, `ArrayList` | `ImmutableList<T>`, `ImmutableArray` |
|Use items first-in-first-out (FIFO)|	`Queue<T>`|	`Queue`|	`ConcurrentQueue<T>`, `ImmutableQueue<T>` |
|Use data Last-In-First-Out (LIFO)|	`Stack<T>`|	`Stack`	| `ConcurrentStack<T>`, `ImmutableStack<T>` |
| Access items sequentially|	`LinkedList<T>`|	`No recommendation`|	`No recommendation`|
|Receive notifications when items are removed or added to the collection. (implements `INotifyPropertyChanged` and `INotifyCollectionChanged`)|	`ObservableCollection<T>`	|`No recommendation`|	`No recommendation`|
|A sorted collection|`SortedList<TKey,TValue>`|`SortedList`	| `ImmutableSortedDictionary<TKey,TValue>`, `ImmutableSortedSet<T>`|
| A set for mathematical functions|	`HashSet<T>` ,`SortedSet<T>`|	`No recommendation`|	`ImmutableHashSet<T>`, `ImmutableSortedSet<T>`|

Another important point that can act as a deciding factor is Algorithmic complexity of [collections](https://docs.microsoft.com/en-us/dotnet/standard/collections/#algorithmic-complexity-of-collections). Before you chose a collection, it is really important that the complexities are taken into account. This can sort out a lot of issues with respect to time complexities before hand.

In addition to this, David Fowler tweeted excellent points on Hashtable, Dictionary, ConcurrentDictionary, ImmutableDictionary
{% twitter 1444467842418548737 %}

- Dictionary
{% twitter 1444467844641484803 %}

- HashTable, ImmutableDictionary
{% twitter 1444467845589454851 %}

- ConcurrentDictionary
{% twitter 1444467843651620866 %}


The motivation for all of this to put into once place, to keep things handy, came when I was working on one of the scenarios to select a collection. I ran into Scott Hanselman's [blog](https://www.hanselman.com/blog/differences-between-hashtable-vs-dictonary-vs-concurrentdictionary-vs-immutabledictionary), where he added a few pointers from David's tweet.

I would keep this handy for the next time I have to work on collections.