All About C# Collections - When To Choose What

The code used in this article is available here on GitHub for download and for your experiments.

By the end of this article, all your queries and doubts will be gone and next time, you will have a fair idea of which collection will be the most appropriate one in a given scenario. I admit it is a long article and still, I didn't split it as in doing that, the momentum would have disappeared. At the same time, you can finish it quickly.

Outline of the article

First, we will see the decision tree about choosing the right collection and after that, a brief introduction of each type will follow with a basic code sample, output, and link to study more.

This self-explanatory decision tree gives a comprehensive guide to use the right collection in the given scenario. The PDF version of this is available here.

C# Collections 
Decision Tree to get a correct collection

How to navigate through the Decision Tree (For Example)

Scenario

We need a generic collection where we can access the items using key and index, both, and all the items should be sorted by default.

Solution

Start -> Only String Storage -> NO -> Access in Sequence -> NO -> Access by Index (only) -> NO -> Access by Key (only) -> NO -> Access by Key or Index or both -> YES -> Default Sorting needed -> YES and the right collection is SortedList<TKey, TValue>. A similar technique can be applied for any situation.

Now, we will explore more categories and respective collections. 

Category #1 - Storing Strings only

StringCollection

Namespace - System.Collections.Specialized

Basic characteristics

  • Only a string can be added to this collection
  • It allows duplicate values to be inserted.
  • We can store NULL values as well.
  • String comparisons are case sensitive.
  • Values can also be accessed by zero-based indexing.
  • Values are stored in the order of insertion.

Code Example

  1. public static void LearnStringCollection()  
  2.        {  
  3.            Console.WriteLine("From LearnStringCollection Method");  
  4.            System.Collections.Specialized.StringCollection strCollection  
  5.                = new System.Collections.Specialized.StringCollection();  
  6.            String[] arString = new String[] { "ATUL""THERAN" };  
  7.            strCollection.AddRange(arString);  
  8.            strCollection.Add("atul");      //Adding same in another case  
  9.            strCollection.Add("THERAN");    //Adding duplicate  
  10.   
  11.            foreach (var item in strCollection)  
  12.            {  
  13.                Console.WriteLine(item);  
  14.            }  
  15.            for (int i = 0; i < strCollection.Count; i++)  
  16.            {  
  17.                Console.WriteLine($"Value at index # {i} is {strCollection[i]}");  
  18.            }  
  19.        }  

OUTPUT

C# Collections 

Read more on MSDN

StringDictionary

Namespace - System.Collections.Specialized

Basic Characteristics

  • Key implements the hash table.
  • Stores only string values in Key-Value pair.
  • Key can’t be null or duplicate while value can be duplicate.
  • It may have one key as String.Empty or “”.
  • Key is case insensitive and stored in lower case.
  • Values are not shown in the order they are stored, while they are shown in the order of the hashed value of the key. (See the example)

Code Example

  1. public static void LearnStringDictionary()  
  2.        {  
  3.            Console.WriteLine("From LearnStringDictionary Method");  
  4.            System.Collections.Specialized.StringDictionary strDict = new System.Collections.Specialized.StringDictionary();  
  5.            strDict.Add("Title Case""Atul");  
  6.            strDict.Add("Lower Case""atul");  
  7.            strDict.Add(string.Empty, "ATUL");  
  8.   
  9.            foreach (System.Collections.DictionaryEntry item in strDict)  
  10.                Console.WriteLine("   {0,-25} {1}", item.Key, item.Value);  
  11.        }  

Output

C# Collections 

Read more on MSDN

Category #2 - Access in Sequence

Last in First Out (LIFO)

Stack – it comes with two versions - Stack class and Stack generic class

Namespace - System.Collections

Characteristics

  • Basically, it has 3 main operations
    • Push – adding a new item to Stack
    • Pop – gets the newest item from the stack and delete it
    • Peek – Next item to be deleted (For stack, this is the newest item added in the stack)
  • Stack class adds all items in object type while Queue generic class becomes specific to the type specified in the declaration.

Code example

  1. static void LearnStackCollection()  
  2.       {  
  3.           Console.WriteLine("from LearnStackCollection Method");  
  4.           Stack<string> stack = new Stack<string>();  
  5.           stack.Push("Atul");         // Adding first item  
  6.           stack.Push("Kumar");        // Adding second item  
  7.           stack.Push("Sharma");       // Adding third item  
  8.           stack.Pop();                // Pop   
  9.           Console.WriteLine($"Peek value ={stack.Peek()}");  //top most item but no delete  
  10.           foreach (var item in stack)  
  11.           {  
  12.               Console.WriteLine(item.ToString());  
  13.           }  
  14.       }  

Output

C# Collections 

Read more on Stack and Generic Stack

First in First Out (FIFO)

Queue – it comes in two versions - Queue class and Queue generic class

Namespace - System.Collections

Characteristics

  • Basically, it has 3 main operations
    • Enqueue – adding a new item to the queue
    • Dequeue – gets the oldest item from the queue and deletes it
    • Peek – Next item to be deleted (For Queue, it is the oldest item at the moment)
  • Queue class adds all items in object type while Queue generic class becomes specific to the type specified in the declaration.

Code example

  1. static void LearnQueueCollection()  
  2.         {  
  3.             Console.WriteLine("from LearnQueueCollection Method");  
  4.             Queue<string> queue = new Queue<string>();  
  5.             queue.Enqueue("Atul");      // Adding first item  
  6.             queue.Enqueue("Kumar");     //Adding second item  
  7.             queue.Enqueue("Sharma");    //Adding third item  
  8.             queue.Dequeue();            // Dequeue and delete from the collection  
  9.             Console.WriteLine($"Peek value ={queue.Peek()}");  //returns the old item but doesn't delete it  
  10.             foreach (var item in queue)  
  11.             {  
  12.                 Console.WriteLine(item.ToString());  
  13.             }  
  14.         }  

Output

C# Collections 

Read more on Queue and Generic Queue.

First to Last or Last to First

Linkedlist<T> – It represents the doubly linked list

Namespace System.Collections.Generic

Characteristics

  • LinkedList<T> accepts null as a valid Value property for reference types and allows duplicate values
  • It is doubly LinkedList so all nodes will have next and previous.
  • If the LinkedList<T> is empty, the First and Last properties contain null.

Code Example

  1. static void LearnLinkedList()  
  2.         {  
  3.             Console.WriteLine("from LearnLinkedList Method");  
  4.             //NOTE - No Knowledge to Tom Cruise Mission Impossible Series wont affect your learning  
  5.             List<string> movies = new List<string>() {"MI-1""MI-2","MI-3","MI-Rougue Nation",};  
  6.             LinkedList<string> movieSeries = new LinkedList<string>(movies);  
  7.             Console.WriteLine("Initial List");  
  8.             foreach (var item in movieSeries)  
  9.             {  
  10.                 Console.WriteLine(item);  
  11.             }  
  12.             // Tasks to do - I have missed Ghost Protocol at # 4 and So far the last movie # 6 Fallout in the series  
  13.             LinkedListNode<string> currentMovie = movieSeries.FindLast("MI-Rougue Nation");  
  14.             movieSeries.AddBefore(currentMovie, "MI-Ghost Protocol");  
  15.             movieSeries.AddAfter(currentMovie, "MI-Fallout");  
  16.             Console.WriteLine("Corrected List");  
  17.             foreach (var item in movieSeries)  
  18.             {  
  19.                 Console.WriteLine(item);  
  20.             }  
  21.         }  

Output

C# Collections 

Read More - MSDN

Category #3: Access by Index

StringCollection – We already covered in Category 1 i.e. 1.1

Array

Namespace – System

Basic Characteristics

  • Array is not a part of System.Collection but it implements IList.
  • It can store the same type of items.
  • It needs to declare the size with the declaration.
  • Increasing the size of an array can be costly.
  • It works on the zero-based index.
  • By default, the maximum size of an array can be 2 GB though it can be increased even more.

Code Example

  1. static void LearnTraditonalArray()  
  2.         {  
  3.             Console.WriteLine("from LearnTraditonalArray  Method");  
  4.             string[] writers = new string[5] { "Atul""Tuba""Anonymous""Theran""Jay" };  
  5.             for (int i = 0; i < writers.Length; i++)  
  6.             {  
  7.                 Console.WriteLine($"{writers[i]} joined at number # {i + 1}");  
  8.             }  
  9.         }  

Output

C# Collections 

Read more on MSDN

ArrayList

Namespace – System.Collection

Basic Characteristics

  • It can store any type of item in it.
  • Capacity defines the count of items it can store without resizing.
  • All items are converted in to object before storing.
  • While fetching, they need to be converted back to the specific type from an object.
  • Boxing/unboxing is a major reason for performance pitfalls of the array list.

Code Example

  1. static void LearnArrayList()  
  2.        {  
  3.            Console.WriteLine("from LearnArrayList  Method");  
  4.            ArrayList arInfo = new ArrayList();  
  5.            arInfo.Add("Till now, Atul has written "); // Adding string  
  6.            arInfo.Add(10);                             //Adding integer  
  7.            arInfo.Add(" Articles");  
  8.            Console.WriteLine($"Currently, It has {arInfo.Count} items and it has Capacity of {arInfo.Capacity}"); //Adding string  
  9.            for (int i = 0; i < arInfo.Count; i++)  
  10.            {  
  11.                Console.Write(arInfo[i].ToString());  
  12.            }  
  13.            Console.WriteLine();  
  14.        }  

Output

C# Collections 

Read More on MSDN

List<T>

Namespace – System.Collections.Generic

Characteristics

  • This is a generic version of ArrayList with more features and flexibilities
  • It can only store the defined type.
  • By default, they are not guaranteed to be sorted. Need to explicitly call sort method.
  • Capacity defines the count of items it can store without resizing.
  • It allows duplicates.
  • It also accepts Null value for a reference type.

Code Example

  1. static void LearnGenericList()  
  2.      {  
  3.          Console.WriteLine("from LearnGenericList Method");  
  4.          List<int> iCounts = new List<int>();  
  5.          iCounts.Add(1);  
  6.          iCounts.Add(12);  
  7.          iCounts.Add(13);  
  8.          Console.WriteLine($"Capacity {iCounts.Capacity}"  );  
  9.          for (int i = 0; i < iCounts.Count; i++)  
  10.          {  
  11.              Console.WriteLine(iCounts[i]);  
  12.          }  
  13.      }  

Output

C# Collections 

More Study -MSDN

Category #4: Access by Key

Hash table

Namespace – System.Collection

Characteristics

  • Each element in Key-Value pair is stored as DictionaryEntry object.
  • Data is searched and retrieved based on the hash code of the key, not the order they are inserted.
  • Key object must be immutable if it is used as a key.
  • Key and Value must be an object type, it means boxing and unboxing of added items happens.

Code Example

  1. static void LearnHashTable()  
  2.        {  
  3.            Console.WriteLine("from LearnHashTable  Method");  
  4.            Hashtable htData = new Hashtable();  
  5.            htData.Add("Name""Atul");            //Inserting string  
  6.            htData.Add("Age", 100);                // inserting integer  
  7.            htData.Add("IsSeniorCitizen"true);   // Inserting boolean  
  8.            htData["Country"] = "USA";             //Another way to inserting the data   
  9.   
  10.            foreach (DictionaryEntry item in htData)  
  11.            {  
  12.                Console.WriteLine($"Value for Key {item.Key} is {item.Value}");  
  13.            }  
  14.        }  

Output

C# Collections 

Read More on MSDN

Sorted List – It will be covered in Category # 5 i.e. 5.4

ListDictionary 

NameSpace – System.Collections.Specialized

Basic Characteristics

  • It is faster than Hashtable as it doesn’t generate the hash code of the key.
  • Faster and well suited for search with a key.
  • It is not appropriate for a large amount of data.
  • Here Key – value pairs are stored as DictionaryEntry object.
  • It also accepts Key and Value to be an object type, hence boxing and unboxing overhead exist.

Code Example

  1. static void LearnListDictionary()  
  2.         {  
  3.             Console.WriteLine("from LearnListDictionary  Method");  
  4.             System.Collections.Specialized.ListDictionary lstData = new System.Collections.Specialized.ListDictionary();  
  5.             lstData.Add("Name""Atul");            //Inserting string  
  6.             lstData.Add("Age", 100);                // inserting age as integer, but not correct  
  7.             lstData.Add("IsSeniorCitizen"true);   // Inserting boolean  
  8.             lstData["Country"] = "USA";             //Another way to inserting the data   
  9.             foreach (DictionaryEntry item in lstData)  
  10.             {  
  11.                 Console.WriteLine($"Value for Key {item.Key} is {item.Value}");  
  12.             }  
  13.         }  

Output

C# Collections 

Read More on MSDN

StringDictionary – Already covered in Category # 1 i.e. 1.2

Dictionary<TKey,TValue>

Namespace – System.Collections.Generic

Basic Characteristics

  • It is the generic version of ListDictionary.
  • Key is hash coded as it is implemented as the Hash Table, different from ListDictionary.
  • All keys should be unique and not null.
  • If keys are strings, they will be case sensitive.
  • It doesn’t allow to add any other type than declared.
  • Key/Values pairs are stored as a KeyValue Pair object.

Code Example

  1. static void LearnGenericDictionary()  
  2.         {  
  3.             Console.WriteLine("from LearnGenericDictionary  Method");  
  4.             Dictionary<stringint> dictExperiences = new Dictionary<stringint>();  
  5.             dictExperiences.Add("Atul", 12);  
  6.             dictExperiences.Add("Theran", 16);  
  7.             dictExperiences.Add("Tuba", 8);  
  8.             dictExperiences.Add("tuba", 5); //Keys are Case sensitive So it will be OK   
  9.             foreach (var item in dictExperiences)  
  10.             {  
  11.                 Console.WriteLine($"{item.Key} has {item.Value} years on experience");  
  12.             }  
  13.         }  

Output

C# Collections 

Read More on MSDN

SortedDictionary<TKey,TValue>

Namespace – System.Collections.Generic

Basic Characteristics

  • It is the same as Dictionary.
  • Items are sorted by the key.

Code Example

  1. static void LearnSortedDictionary()  
  2.       {  
  3.           Console.WriteLine("from LearnSortedDictionary  Method");  
  4.           SortedDictionary<intstring> srtMembers =  
  5.          new SortedDictionary<intstring>();  
  6.           srtMembers.Add(1, "Theran");  
  7.           srtMembers.Add(4, "Tuba");  
  8.           srtMembers.Add(2, "Jay");  
  9.           srtMembers.Add(3, "Atul");  
  10.           //Items are inserted in different order (1,4,2,3) but sorted in 1,2,3,4   
  11.           foreach (var item in srtMembers)  
  12.           {  
  13.               Console.WriteLine($"Member # {item.Key} is {item.Value}");  
  14.           }  
  15.   
  16.       }  

Output

C# Collections

Read more on MSDN

Category #5: Access by Key or Index or Both

NameValueCollection

Namespace - System.Collections.Specialized

Characteristics

  • It can store strings only as key and Value pair.
  • It allows duplicate keys and stores the value together.
  • No Particular ordering or sorting guaranteed.
  • It accepts the null as a key or as a value.

Code Example

  1. static void LearnNameValueCollection()  
  2.       {  
  3.           Console.WriteLine("from LearnNameValueCollection Method");  
  4.           NameValueCollection nvCityCountry = new NameValueCollection();  
  5.           nvCityCountry.Add("IND""Mumbai");  
  6.           nvCityCountry.Add("USA""New York");  
  7.           nvCityCountry.Add("USA""Chicago"); //Duplicate key and see the output  
  8.           nvCityCountry.Add("USA""Chicago"); // Duplicate key and value both  
  9.           Console.WriteLine("Access By index");   
  10.           for (int i = 0; i < nvCityCountry.Count; i++)  
  11.           {  
  12.               Console.WriteLine("   [{0}]     {1,-10} {2}", i, nvCityCountry.GetKey(i), nvCityCountry.Get(i));  
  13.           }  
  14.           Console.WriteLine("Access By Key");  
  15.           foreach (string item in nvCityCountry.AllKeys)  
  16.           {  
  17.               Console.WriteLine("   {0,-10} {1}", item, nvCityCountry[item]);  
  18.           }  
  19.       }  

Output

C# Collections 

Read more here at MSDN

SortedList <TKey, TValue>

Namespace - System.Collections.Generic

Characteristics

  • It has all the same properties, like SortedDictionary<TKey, TValue>
  • SortedList uses less memory than SortedDictionary.
  • SortedDictionary has faster insertion and removal operations for unsorted data, O(log n) as opposed to O(n) for SortedList.
  • If the list is populated all at once from sorted data, SortedList is faster than SortedDictionary.

Code Example

  1. static void LearnSortedListGeneric()  
  2.        {  
  3.            Console.WriteLine("from LearnSortedListGeneric Method");  
  4.            SortedDictionary<intstring> classRoll =  
  5.            new SortedDictionary<intstring>();  
  6.            classRoll.Add(1, "Atul");  
  7.            classRoll.Add(4, "Jay");  
  8.            classRoll.Add(3, "Theran");  
  9.            classRoll.Add(2, "Tuba");  
  10.            //By default it sorts  
  11.            foreach (var item in classRoll)  
  12.            {  
  13.                Console.WriteLine(item.Value);  
  14.            }  
  15.            //Access by Index  
  16.            Console.WriteLine(classRoll[2]);  
  17.            //Access by Key  
  18.            foreach (KeyValuePair<intstring> kvp in classRoll)  
  19.            {  
  20.                Console.WriteLine("Key = {0}, Value = {1}",  
  21.                    kvp.Key, kvp.Value);  
  22.            }  
  23.        }  

Output

C# Collections 

More study - MSDN

NameObjectCollectionBase

Namespace - System.Collections.Specialized

Characteristics

  • This is an abstract class so it can be used directly like other collections.
  • Key is stored as a string while Value can be any object which is accessible as Key or Index.
  • Underlying structure for this class is the Hash Table.

Code Example , output and further details are available Here on MSDN

KeyedCollection<TKey, TValue>

Namespace - System.Collections.ObjectModel

Characteristics 

  • Like NameObjectCollectionBase, is this also an abstract class.
  • It is generic version of NameObjectCollectionBase.
  • Unlike other dictionary collection, in KeyedCollection values are NOT stored in Key value pair; rather, they are stored as one value where a key is embedded into the value.

Code example, output, and further details are available here on MSDN.

I hope after this long read, I was able to explain and give a fair idea of which collection to use in which scenario. What do you think? Please, share your feedback. The code used in this article is available here on GitHub for download and for your experiments.


Similar Articles