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

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. foreach (var item in strCollection)
  11. {
  12. Console.WriteLine(item);
  13. }
  14. for (int i = 0; i < strCollection.Count; i++)
  15. {
  16. Console.WriteLine($"Value at index # {i} is {strCollection[i]}");
  17. }
  18. }

OUTPUT

C# Collections

Read more on MSDN

StringDictionary

Namespace - System.Collections.Specialized

Basic Characteristics

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. foreach (System.Collections.DictionaryEntry item in strDict)
  9. Console.WriteLine(" {0,-25} {1}", item.Key, item.Value);
  10. }

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

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

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

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

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

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

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

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. foreach (DictionaryEntry item in htData)
  10. {
  11. Console.WriteLine($"Value for Key {item.Key} is {item.Value}");
  12. }
  13. }

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

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

Code Example

  1. static void LearnGenericDictionary()
  2. {
  3. Console.WriteLine("from LearnGenericDictionary Method");
  4. Dictionary<string, int> dictExperiences = new Dictionary<string, int>();
  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

Code Example

  1. static void LearnSortedDictionary()
  2. {
  3. Console.WriteLine("from LearnSortedDictionary Method");
  4. SortedDictionary<int, string> srtMembers =
  5. new SortedDictionary<int, string>();
  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. }

Output

C# Collections

Read more on MSDN

Category #5: Access by Key or Index or Both

NameValueCollection

Namespace - System.Collections.Specialized

Characteristics

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

Code Example

  1. static void LearnSortedListGeneric()
  2. {
  3. Console.WriteLine("from LearnSortedListGeneric Method");
  4. SortedDictionary<int, string> classRoll =
  5. new SortedDictionary<int, string>();
  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<int, string> 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

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

KeyedCollection<TKey, TValue>

Namespace - System.Collections.ObjectModel

Characteristics

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.