Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is the difference between list and dictionary in C#?
A List and Dictionary are both generic collections in C#, but they serve different purposes. A List<T> stores elements in a sequential order with index-based access, while a Dictionary<TKey, TValue> stores key-value pairs for fast lookups.
Understanding when to use each collection type is crucial for writing efficient C# applications. Lists are ideal for ordered data where you need to access elements by position, while dictionaries are perfect for mapping relationships and quick key-based retrieval.
Syntax
Following is the syntax for creating a List −
List<T> listName = new List<T>();
// or with initialization
List<T> listName = new List<T>() { item1, item2, item3 };
Following is the syntax for creating a Dictionary −
Dictionary<TKey, TValue> dictName = new Dictionary<TKey, TValue>();
// or with initialization
Dictionary<TKey, TValue> dictName = new Dictionary<TKey, TValue>() {
{ key1, value1 },
{ key2, value2 }
};
Using List Collection
A List stores elements in sequential order and provides index-based access. Elements can be duplicated and are accessed using zero-based indexing −
using System;
using System.Collections.Generic;
class Program {
public static void Main() {
List<string> subjects = new List<string>() {
"Maths",
"English",
"Science"
};
// Access by index
Console.WriteLine("First subject: " + subjects[0]);
// Add new element
subjects.Add("History");
// Loop through all elements
Console.WriteLine("All subjects:");
for(int i = 0; i < subjects.Count; i++) {
Console.WriteLine((i + 1) + ". " + subjects[i]);
}
}
}
The output of the above code is −
First subject: Maths All subjects: 1. Maths 2. English 3. Science 4. History
Using Dictionary Collection
A Dictionary stores key-value pairs where each key must be unique. It provides fast lookups based on the key −
using System;
using System.Collections.Generic;
class Program {
public static void Main() {
Dictionary<string, int> sports = new Dictionary<string, int>();
// Add key-value pairs
sports.Add("football", 11);
sports.Add("basketball", 5);
sports.Add("tennis", 2);
// Access by key
Console.WriteLine("Football players: " + sports["football"]);
// Check if key exists
if(sports.ContainsKey("rugby")) {
Console.WriteLine("Rugby players: " + sports["rugby"]);
} else {
Console.WriteLine("Rugby not found in dictionary");
}
// Loop through all key-value pairs
Console.WriteLine("All sports:");
foreach(KeyValuePair<string, int> sport in sports) {
Console.WriteLine(sport.Key + ": " + sport.Value + " players");
}
}
}
The output of the above code is −
Football players: 11 Rugby not found in dictionary All sports: football: 11 players basketball: 5 players tennis: 2 players
Comparison
| Feature | List<T> | Dictionary<TKey, TValue> |
|---|---|---|
| Storage | Sequential elements with index | Key-value pairs |
| Access | By index: list[0] | By key: dict["key"] |
| Duplicates | Allows duplicate values | Unique keys, duplicate values allowed |
| Order | Maintains insertion order | No guaranteed order |
| Performance | O(1) index access, O(n) search | O(1) key-based lookup |
Common Use Cases
Use List when:
You need to store items in a specific order
You want to access elements by index position
You need to allow duplicate values
You want simple iteration through all elements
Use Dictionary when:
You need fast lookups based on a unique identifier (key)
You want to associate values with specific keys
You need to check if a specific key exists
You're implementing mappings or relationships between data
Conclusion
Lists provide sequential, index-based storage ideal for ordered collections, while Dictionaries offer fast key-value pair lookups perfect for mapping relationships. Choose Lists for ordered data access and Dictionaries when you need efficient key-based retrieval and unique key constraints.
