In this article we are going to discuss Collection and types of Collections in C#.
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.
Here are some of the most commonly used types of collections in C#
Found in the System.Collections namespace, non-generic collections can store elements of any type.
ArrayList arrayList = new ArrayList();
arrayList.Add(1);
arrayList.Add("Hello");
Hashtable hashtable = new Hashtable();
hashtable.Add(1, "One");
hashtable.Add("Two", 2);
Queue queue = new Queue();
queue.Enqueue("First");
queue.Enqueue("Second");
Stack stack = new Stack();
stack.Push("First");
stack.Push("Second");
Found in the System.Collections.Generic namespace, generic collections are type-safe and provide better performance.
List<int> intList = new List<int>();
intList.Add(1);
intList.Add(2);
Dictionary<int, string> dict = new Dictionary<int, string>();
dict.Add(1, "One");
dict.Add(2, "Two");
Queue<string> queue = new Queue<string>();
queue.Enqueue("First");
queue.Enqueue("Second");
Stack<string> stack = new Stack<string>();
stack.Push("First");
stack.Push("Second");
HashSet<string> hashSet = new HashSet<string>();
hashSet.Add("Apple");
hashSet.Add("Banana");
LinkedList<int> linkedList = new LinkedList<int>();
linkedList.AddLast(1);
linkedList.AddLast(2);
SortedList<int, string> sortedList = new SortedList<int, string>();
sortedList.Add(2, "Two");
sortedList.Add(1, "One");
Found in the System.Collections.Concurrent namespace, these collections are designed for thread-safe operations.
ConcurrentDictionary<int, string> concurrentDict = new ConcurrentDictionary<int, string>();
concurrentDict.TryAdd(1, "One");
ConcurrentQueue<string> concurrentQueue = new ConcurrentQueue<string>();
concurrentQueue.Enqueue("First");
ConcurrentStack<string> concurrentStack = new ConcurrentStack<string>();
concurrentStack.Push("First");
ConcurrentBag<string> concurrentBag = new ConcurrentBag<string>();
concurrentBag.Add("First");
BlockingCollection<string> blockingCollection = new BlockingCollection<string>();
blockingCollection.Add("First");
Found in the System.Collections.Specialized namespace, these collections are designed for specific purposes.
NameValueCollection nameValueCollection = new NameValueCollection();
nameValueCollection.Add("Key1", "Value1");
StringCollection stringCollection = new StringCollection();
stringCollection.Add("First");
BitArray bitArray = new BitArray(8);
bitArray[0] = true;
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.
ImmutableList<int> immutableList = ImmutableList.Create(1, 2, 3);
ImmutableDictionary<int, string> immutableDict = ImmutableDictionary.CreateRange(new[] { new KeyValuePair(1, "One") });
ImmutableHashSet<string> immutableHashSet = ImmutableHashSet.Create("Apple", "Banana");