Collections General Overview
Things become better to work when everything is organized. Same applicable while coding, we want to keep data organized. We can achieve this using Collections
C# provides a rich set of collection classes to store and manage groups of objects efficiently. ๐ฆ These collections are categorized into two primary groups Generic Collections and Non-Generic Collections.
1. Non-Generic Collections: These collections do not have a specific type parameter. They are less type-safe compared to generic collections.
ArrayList ๐
Dynamically resizable array of objects.
Can store elements of any data type.
Less type-safe, as you need to cast elements when retrieving them.
Hashtable ๐๏ธ
Stores key-value pairs.
Keys must be unique.
Not strongly typed, requiring manual casting.
Stack ๐ฅ
Follows Last-In-First-Out (LIFO) order.
Useful for operations like undo/redo.
Queue ๐
Follows First-In-First-Out (FIFO) order.
Ideal for processing items in the order they were added.
2. Generic Collections: These collections are type-safe, meaning you specify the data type of the elements they can hold. They are generally preferred over non-generic collections due to their type safety and performance benefits.
List<T> ๐
Ordered collection of elements.
Supports indexing, adding, removing, and searching elements.
Dictionary<TKey, TValue> ๐
Stores key-value pairs.
Keys must be unique.
Provides efficient lookup based on keys.
HashSet<T> ๐ฒ
Unordered collection of unique elements.
Optimized for fast lookup and removal.
SortedSet<T> ๐
Ordered collection of unique elements.
Elements are sorted automatically.
Stack<T> ๐ฅ
- Generic version of the Stack class.
Queue<T> ๐
- Generic version of the Queue class.
Choosing the Right Collection ๐ฏ
The choice of collection depends on your specific requirements:
Ordered vs. Unordered: If you need to access elements by index or maintain a specific order, use
List<T>
orSortedSet<T>
. For unordered collections, considerHashSet<T>
.Unique vs. Duplicate: If you need to ensure unique elements, use
HashSet<T>
orSortedSet<T>
. For duplicate elements, useList<T>
orDictionary<TKey, TValue>
.Key-Value Pairs: If you need to associate values with keys, use
Dictionary<TKey, TValue>
.LIFO or FIFO: For LIFO behavior, use
Stack<T>
. For FIFO behavior, useQueue<T>
.
Example ๐ก
using System.Collections.Generic;
List<string> names = new List<string>();
names.Add("Alice");
names.Add("Bob");
names.Add("Charlie");
Dictionary<int, string> phoneBook = new Dictionary<int, string>();
phoneBook.Add(123456, "Alice");
phoneBook.Add(789012, "Bob");
HashSet<int> uniqueNumbers = new HashSet<int>();
uniqueNumbers.Add(10);
uniqueNumbers.Add(20);
uniqueNumbers.Add(10); // Duplicate, will not be added
foreach (string name in names)
{
Console.WriteLine(name);
}
Console.WriteLine(phoneBook[123456]);
foreach (int number in uniqueNumbers)
{
Console.WriteLine(number);
}
By understanding these core concepts and the specific use cases of each collection, you can effectively leverage C#'s powerful collection framework to build robust and efficient applications.
using System.Collections.Generic;
List<string> names = new List<string>();
names.Add("Alice");
names.Add("Bob");
names.Add("Charlie");
Dictionary<int, string> phoneBook = new Dictionary<int, string>();
phoneBook.Add(123456, "Alice");
phoneBook.Add(789012, "Bob");
HashSet<int> uniqueNumbers = new HashSet<int>();
uniqueNumbers.Add(10);
uniqueNumbers.Add(20);
uniqueNumbers.Add(10); // Duplicate, will not be added
foreach (string name in names)
{
Console.WriteLine(name);
}
Console.WriteLine(phoneBook[123456]);
foreach (int number in uniqueNumbers)
{
Console.WriteLine(number);
}
By understanding these core concepts and the specific use cases of each collection, you can effectively leverage C#'s powerful collection framework to build robust and efficient applications.