C# List vs Dictionary: Differences, Usage, and Best Practices
A List is a collection of ordered elements accessed by index, while a Dictionary<TKey, TValue> stores key-value pairs for fast lookup by key.
Both List and Dictionary are part of the C# collections framework and are widely used for storing and managing data. A List maintains insertion order and allows duplicate values, making it ideal for sequential data. A Dictionary, on the other hand, uses keys to uniquely identify values, providing fast access and retrieval. Choosing between them depends on how you plan to access and manage your data.
When to Use List or Dictionary?
Use List when:
• You need ordered data
• Access is primarily by index
• Duplicate values are allowed
• You iterate over all items frequently
Use Dictionary when:
• You need fast lookups by key
• Each item must have a unique identifier
• You want key-value relationships
• Performance is important for searches
How to Use List and Dictionary?
List Example
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
List names = new List();
names.Add("Alice");
names.Add("Bob");
names.Add("Alice");
foreach (var name in names)
{
Console.WriteLine(name);
}
}
}
Dictionary Example
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
Dictionary<int, string> users = new Dictionary<int, string>();
users.Add(1, "Alice");
users.Add(2, "Bob");
Console.WriteLine(users[1]);
}
}
Sample Code Snippet
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
List numbers = new List { 1, 2, 3 };
Dictionary<int, string> map = new Dictionary<int, string>
{
{1, "One"},
{2, "Two"},
{3, "Three"}
};
Console.WriteLine(numbers[0]);
Console.WriteLine(map[2]);
}
}
List vs Dictionary Comparison
| Feature | List | Dictionary |
|---|---|---|
| Structure | Ordered collection | Key-value pairs |
| Access | By index | By key |
| Lookup Speed | O(n) | O(1) average |
| Duplicates | Allowed | Keys must be unique |
| Ordering | Maintains order | No guaranteed order |
Advantages and Disadvantages
| Collection | Advantages | Disadvantages |
|---|---|---|
| List | Simple to use, maintains order, flexible | Slow lookups for large data |
| Dictionary | Fast lookups, efficient for large datasets | No index-based access, more memory usage |
Similar and Alternative Options
| Option | Description | When to Use |
|---|---|---|
| Array | Fixed-size collection | When size is known and constant |
| HashSet | Unique values collection | When duplicates are not allowed |
| SortedDictionary | Sorted key-value pairs | When order by key is needed |
| Queue/Stack | Specialized collections | FIFO or LIFO scenarios |
Common Mistakes
• Using List when fast lookup is required
• Using Dictionary without checking if a key exists
• Accessing Dictionary with a missing key (causes exception)
• Forgetting that Dictionary does not guarantee order
• Overusing Dictionary for small datasets where List is simpler
• Not choosing the right collection based on use case