C# Collections – Quick Revision
1. What is a Collection in C#?
A Collection in C# is used to store, manage, and manipulate multiple objects or values in a single variable.
Collections provide built-in methods for adding, removing, searching, sorting, and accessing elements.
Example:
List<int> numbers = new List<int> { 10, 20, 30 };
numbers.Add(40);
numbers.Remove(20);
foreach (int number in numbers)
{
Console.WriteLine(number);
}
2. Types of Collections in C#
C# Collections
│
├── 1. Non-Generic Collections
│ ├── ArrayList
│ ├── Hashtable
│ ├── Stack
│ └── Queue
│
├── 2. Generic Collections
│ ├── List<T>
│ ├── Dictionary<TKey, TValue>
│ ├── HashSet<T>
│ ├── SortedSet<T>
│ ├── Stack<T>
│ ├── Queue<T>
│ └── LinkedList<T>
│
├── 3. Concurrent Collections
│ ├── ConcurrentDictionary
│ ├── ConcurrentQueue
│ ├── ConcurrentStack
│ └── ConcurrentBag
│
└── 4. Immutable Collections
├── ImmutableList
├── ImmutableDictionary
└── ImmutableHashSet
3. Important Collections with Examples
|
Collection |
Description |
Use Case |
|---|---|---|
|
|
Dynamic-sized list |
Employee records |
|
|
Key-value pairs |
Employee ID and name |
|
|
Stores unique values |
Remove duplicates |
|
|
Unique values in sorted order |
Sorted unique IDs |
|
|
LIFO (Last In, First Out) |
Undo operation |
|
|
FIFO (First In, First Out) |
Job processing |
|
|
Doubly linked nodes |
Frequent node insertion/removal |
|
|
Non-generic dynamic list |
Legacy applications |
|
|
Non-generic key-value collection |
Legacy applications |
4. Generic vs Non-Generic Collections
|
Generic |
Non-Generic |
|---|---|
|
Type-safe |
Stores values as |
|
Better type checking |
Requires casting when retrieving values |
|
Avoids boxing for value types |
May cause boxing/unboxing |
|
|
|
|
Recommended for modern C# |
Mainly used in legacy code |
5. Common Collection Interfaces
|
Interface |
Purpose |
|---|---|
|
|
Supports iteration using |
|
|
Supports Add, Remove, Count |
|
|
Supports index-based access |
|
|
Represents key-value collections |
|
|
Provides read-only collection access |
|
|
Provides read-only index-based access |
|
|
Supports query composition through providers such as EF Core |
6. Important Interview Questions
Q1. What is the difference between Array and List?
Array has a fixed size, whereas List<T> can grow or shrink dynamically.
Q2. List vs Dictionary?
List<T> stores elements accessed by index. Dictionary<TKey,TValue> stores key-value pairs and supports fast key-based lookup.
Q3. HashSet vs List?
HashSet<T> stores unique elements, whereas List<T> allows duplicates and maintains insertion order.
Q4. Stack vs Queue?
Stack follows LIFO, whereas Queue follows FIFO.
Q5. IEnumerable vs IQueryable?
IEnumerable<T> supports iteration over data. IQueryable<T> represents queries that a provider, such as EF Core, can translate into SQL.
Q6. Which collection is thread-safe?
Collections such as ConcurrentDictionary, ConcurrentQueue, and ConcurrentBag support thread-safe operations.
Q7. Which collection is commonly used in ASP.NET Core?
List<T> and Dictionary<TKey,TValue> are common. EF Core also frequently uses IQueryable<T> for database queries.
Key Points:
-
Prefer generic collections for type safety.
-
Use
List<T>for ordered, index-based data. -
Use
Dictionary<TKey,TValue>for key-based lookup. -
Use
HashSet<T>for uniqueness. -
Use concurrent collections for multithreaded access.
-
Use immutable collections when data should not change after creation.