Collections in C#


In this article we are going to discuss Collection and types of Collections in C#.

C# Collections Introduction

In C#, collections are used to store, manage and manipulate groups of related data. Collections are more flexible than arrays as they provide various methods to efficiently insert, delete, and manage data.

Types of Collections in C#

Here are some of the most commonly used types of collections in C#

Collections in C#

1. Non-Generic Collections

 Found in the System.Collections namespace, non-generic collections can store elements of any type.

  1. ArrayList - A dynamically resizable array.
    ArrayList arrayList = new ArrayList();
    arrayList.Add(1);
    arrayList.Add("Hello");
  2. Hashtable - A collection of key-value pairs with unique keys.
    Hashtable hashtable = new Hashtable();
    hashtable.Add(1, "One");
    hashtable.Add("Two", 2);
  3. Queue - A first-in, first-out (FIFO) collection.
    Queue queue = new Queue();
    queue.Enqueue("First");
    queue.Enqueue("Second");
  4. Stack - A last-in, first-out (LIFO) collection.
    Stack stack = new Stack();
    stack.Push("First");
    stack.Push("Second");

2. Generic Collections

Found in the System.Collections.Generic namespace, generic collections are type-safe and provide better performance.

  1. List<T> - A strongly-typed list of objects.
    List<int> intList = new List<int>();
    intList.Add(1);
    intList.Add(2);
  2. Dictionary<TKey, TValue> - A collection of key-value pairs.
    Dictionary<int, string> dict = new Dictionary<int, string>();
    dict.Add(1, "One");
    dict.Add(2, "Two");
  3. Queue<T> - A strongly-typed FIFO collection.
    Queue<string> queue = new Queue<string>();
    queue.Enqueue("First");
    queue.Enqueue("Second");
  4. Stack<T> - A strongly-typed LIFO collection.
    Stack<string> stack = new Stack<string>();
    stack.Push("First");
    stack.Push("Second");
  5. HashSet<T> - A collection of unique elements.
    HashSet<string> hashSet = new HashSet<string>();
    hashSet.Add("Apple");
    hashSet.Add("Banana");
  6. LinkedList<T> - A doubly linked list.
    LinkedList<int> linkedList = new LinkedList<int>();
    linkedList.AddLast(1);
    linkedList.AddLast(2);
  7. SortedList<TKey, TValue> - A collection that stores key-value pairs in sorted order by key.
    SortedList<int, string> sortedList = new SortedList<int, string>();
    sortedList.Add(2, "Two");
    sortedList.Add(1, "One");

3. Concurrent Collections

Found in the System.Collections.Concurrent namespace, these collections are designed for thread-safe operations.

  1. ConcurrentDictionary<TKey, TValue> - A thread-safe collection of key-value pairs.
    ConcurrentDictionary<int, string> concurrentDict = new ConcurrentDictionary<int, string>();
    concurrentDict.TryAdd(1, "One");
  2. ConcurrentQueue<T> - A thread-safe FIFO collection.
    ConcurrentQueue<string> concurrentQueue = new ConcurrentQueue<string>();
    concurrentQueue.Enqueue("First");
  3. ConcurrentStack<T> - A thread-safe LIFO collection.
    ConcurrentStack<string> concurrentStack = new ConcurrentStack<string>();
    concurrentStack.Push("First");
  4. ConcurrentBag<T> - A thread-safe, unordered collection of elements.
    ConcurrentBag<string> concurrentBag = new ConcurrentBag<string>();
    concurrentBag.Add("First");
  5. BlockingCollection<T> - A thread-safe collection that provides blocking and bounding capabilities.
    BlockingCollection<string> blockingCollection = new BlockingCollection<string>();
    blockingCollection.Add("First");

4. Specialized Collections

Found in the System.Collections.Specialized namespace, these collections are designed for specific purposes.

  1. NameValueCollection - A collection that stores string keys and multiple string values under each key.
    NameValueCollection nameValueCollection = new NameValueCollection();
    nameValueCollection.Add("Key1", "Value1");
  2. StringCollection - A collection that stores strings.
    StringCollection stringCollection = new StringCollection();
    stringCollection.Add("First");
  3. BitArray - A collection that stores boolean values.
    BitArray bitArray = new BitArray(8);
    bitArray[0] = true;

5. Immutable Collections

Found in the System.Collections.Immutable namespace, these collections are immutable and provide thread-safety by ensuring that their elements cannot be modified after creation.

  1. ImmutableList<T> - An immutable list.
    ImmutableList<int> immutableList = ImmutableList.Create(1, 2, 3);
  2. ImmutableDictionary<TKey, TValue> - An immutable dictionary.
    ImmutableDictionary<int, string> immutableDict = ImmutableDictionary.CreateRange(new[] { new KeyValuePair(1, "One") });
  3. ImmutableHashSet<T> - An immutable hash set.
    ImmutableHashSet<string> immutableHashSet = ImmutableHashSet.Create("Apple", "Banana");

Prev Next