Generic collections are type-safe data structures that store and manage elements of specified types. They are available primarily in the System.Collections.Generic namespace and provide efficient ways to organize, access, and manipulate data.
- Type Safety: Ensures that only elements of the specified type can be added to a collection.
- Better Performance: Avoids boxing and unboxing when storing and retrieving value types, unlike non-generic collections that use object.
- Flexibility: Provides different collection types for lists, key-value pairs, unique elements, and ordered processing.
For example, List<int> stores integers, while Dictionary<int, string> stores integer keys associated with string values.
using System;
using System.Collections.Generic;
class Geeks{
static void Main(){
List<string> names = new List<string>();
names.Add("Alice");
names.Add("Bob");
foreach (string name in names){
Console.WriteLine(name);
}
}
}
Output
Alice Bob
Here, List<string> accepts only string values. Attempting to add an integer directly to the list results in a compile-time error.
Common Types of Generic Collections
C# provides several generic collection classes, each designed for different data storage and access requirements.

1. List<T>
List<T> is a dynamically sized collection that stores elements in order and allows access using an index. It automatically grows as elements are added.
Key Features:
- Maintains the order of elements and allows duplicates.
- Supports indexed access, searching, and sorting.
- Automatically increases capacity when necessary.
using System;
using System.Collections.Generic;
class Geeks{
static void Main()
{
List<string> names = new List<string>();
names.Add("Alice");
names.Add("Bob");
Console.WriteLine(names[0]);
Console.WriteLine(names.Count);
}
}
Output
Alice 2
2. Dictionary<TKey, TValue>
Dictionary<TKey, TValue> stores data as key-value pairs, allowing efficient retrieval of values using their corresponding keys. Each key must be unique.
Key Features:
- Stores data as key-value pairs.
- Requires unique keys but allows duplicate values.
- Provides fast key-based lookup on average.
using System;
using System.Collections.Generic;
class Geeks{
static void Main()
{
Dictionary<int, string> users = new Dictionary<int, string>();
users.Add(1, "Alice");
users.Add(2, "Bob");
Console.WriteLine(users[2]);
}
}
Output
Bob
3. Queue<T>
Queue<T> follows the First-In, First-Out (FIFO) principle, where the first element added is the first element removed. It is commonly used for task processing and request handling.
Key Features:
- Processes elements in FIFO order.
- Uses Enqueue() to add and Dequeue() to remove elements.
- Uses Peek() to view the next element without removing it.
using System;
using System.Collections.Generic;
class Geeks{
static void Main()
{
Queue<int> numbers = new Queue<int>();
numbers.Enqueue(10);
numbers.Enqueue(20);
Console.WriteLine(numbers.Dequeue());
Console.WriteLine(numbers.Peek());
}
}
Output
10 20
4. Stack<T>
Stack<T> follows the Last-In, First-Out (LIFO) principle, where the last element added is the first element removed. It is useful for undo operations, expression evaluation, and backtracking.
Key Features:
- Processes elements in LIFO order.
- Uses Push() to add and Pop() to remove elements.
- Uses Peek() to access the top element without removing it.
using System;
using System.Collections.Generic;
class Geeks{
static void Main()
{
Stack<string> pages = new Stack<string>();
pages.Push("Home");
pages.Push("Profile");
Console.WriteLine(pages.Pop());
Console.WriteLine(pages.Peek());
}
}
Output
Profile Home
5. HashSet<T>
HashSet<T> stores unique elements and prevents duplicate entries. It is commonly used when checking membership or performing mathematical set operations.
Key Features
- Stores only unique elements.
- Does not guarantee a particular iteration order.
- Supports union, intersection, and difference operations.
using System;
using System.Collections.Generic;
class Geeks{
static void Main()
{
HashSet<int> numbers = new HashSet<int>();
numbers.Add(10);
numbers.Add(20);
numbers.Add(10);
Console.WriteLine(numbers.Count);
Console.WriteLine(numbers.Contains(20));
}
}
Output
2 True
6. SortedList<TKey, TValue>
SortedList<TKey, TValue> stores key-value pairs in ascending order of keys, using the default comparer or a supplied comparer. It supports accessing entries by key and by index through its key and value lists.
Key Features:
- Maintains entries in sorted key order.
- Requires unique keys.
- Supports lookup by key and indexed access to keys and values.
using System;
using System.Collections.Generic;
class Geeks{
static void Main()
{
SortedList<int, string> students = new SortedList<int, string>();
students.Add(3, "Charlie");
students.Add(1, "Alice");
students.Add(2, "Bob");
foreach (var student in students)
{
Console.WriteLine($"{student.Key}: {student.Value}");
}
}
}
Output
1: Alice 2: Bob 3: Charlie
Advantages of Generic Collections
- Type Safety: Prevents adding elements of incompatible types at compile time.
- Code Reusability: Allows the same collection class to work with different data types.
- Performance: Avoids boxing and unboxing for value types and provides efficient operations according to the chosen collection.
- Built-in Operations: Supports common operations such as adding, removing, searching, and iterating over elements.
Choosing the Right Generic Collection
Choosing the appropriate generic collection depends on how data needs to be stored, accessed, and processed.
Collection | Best Used For |
|---|---|
Ordered elements and index-based access | |
Fast key-value lookups | |
FIFO task processing | |
LIFO operations and undo functionality | |
Unique elements and set operations | |
Key-value data maintained in sorted order | |
Frequent insertion or removal at known nodes | |
Sorted key-value data with frequent insertions and removals | |
Unique elements maintained in sorted order |