Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Dictionaries vs. Lists: Choosing the Right Tool | Dictionaries and Key-Value Collections
C# Lists & Collections

bookDictionaries vs. Lists: Choosing the Right Tool

When working with collections in C#, you often need to choose between using a List<T> or a Dictionary<TKey, TValue>. Each type offers distinct advantages and is suited to different scenarios. The following table presents a direct comparison to help you decide which is best for your needs:

FeatureListDictionary<TKey, TValue>
Lookup SpeedO(n) β€” must check each itemO(1) β€” direct access by key
Memory UsageLower (stores only values)Higher (stores keys and values, plus hashing overhead)
Duplicate ValuesAllowedValues allowed, but keys must be unique
Key-based AccessNot supportedSupported (fast lookup by key)
Index-based AccessSupported (by position)Not supported (no order guarantee)
Typical Use CasesOrdered lists, simple collectionsFast lookups, mapping unique keys to values
Program.cs

Program.cs

copy
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
using System; using System.Collections.Generic; namespace ConsoleApp { public class Program { public static void Main(string[] args) { // List of users (each user is a tuple of ID and Name) var usersList = new List<(int Id, string Name)> { (1, "Alice"), (2, "Bob"), (3, "Charlie"), }; // Search for user with ID 2 in the list string foundName = null; foreach (var user in usersList) { if (user.Id == 2) { foundName = user.Name; break; } } Console.WriteLine("List search found: " + foundName); // Dictionary of users (ID as key, Name as value) var usersDict = new Dictionary<int, string> { {1, "Alice"}, {2, "Bob"}, {3, "Charlie"}, }; // Search for user with ID 2 in the dictionary string dictName = null; if (usersDict.TryGetValue(2, out dictName)) { Console.WriteLine("Dictionary search found: " + dictName); } } } }

When you switch from a list to a dictionary for lookups, you gain significant speed improvements for large collections. However, dictionaries do use more memory because they must store keys, values, and maintain a hash table to enable fast access. If your data is small or you need to preserve order or allow duplicates, a list might be simpler and more memory-efficient. For large datasets where you need to find items by a unique identifier quickly, a dictionary is almost always the better choice.

When you switch from a list to a dictionary for lookups, you gain significant speed improvements for large collections. However, dictionaries do use more memory because they must store keys, values, and maintain a hash table to enable fast access. If your data is small or you need to preserve order or allow duplicates, a list might be simpler and more memory-efficient. For large datasets where you need to find items by a unique identifier quickly, a dictionary is almost always the better choice.

Program.cs

Program.cs

copy
123456789101112131415161718192021222324252627282930313233343536373839404142434445
using System; using System.Collections.Generic; namespace ConsoleApp { public class Program { public static void Main(string[] args) { // Original approach: searching a list for a product by ID var productsList = new List<(int Id, string Name)> { (101, "Laptop"), (102, "Phone"), (103, "Tablet"), }; string productName = null; foreach (var product in productsList) { if (product.Id == 102) { productName = product.Name; break; } } Console.WriteLine("List search: " + productName); // Refactored approach: using a dictionary for fast lookup var productsDict = new Dictionary<int, string> { {101, "Laptop"}, {102, "Phone"}, {103, "Tablet"}, }; string dictProductName = null; if (productsDict.TryGetValue(102, out dictProductName)) { Console.WriteLine("Dictionary lookup: " + dictProductName); } } } }

1. When is a dictionary more efficient than a list?

2. What is a drawback of using dictionaries?

question mark

When is a dictionary more efficient than a list?

Select the correct answer

question mark

What is a drawback of using dictionaries?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 6

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you give examples of when to use a list versus a dictionary in real-world C# applications?

What are some common mistakes when choosing between lists and dictionaries?

How do I convert a list to a dictionary in C#?

bookDictionaries vs. Lists: Choosing the Right Tool

Swipe to show menu

When working with collections in C#, you often need to choose between using a List<T> or a Dictionary<TKey, TValue>. Each type offers distinct advantages and is suited to different scenarios. The following table presents a direct comparison to help you decide which is best for your needs:

FeatureListDictionary<TKey, TValue>
Lookup SpeedO(n) β€” must check each itemO(1) β€” direct access by key
Memory UsageLower (stores only values)Higher (stores keys and values, plus hashing overhead)
Duplicate ValuesAllowedValues allowed, but keys must be unique
Key-based AccessNot supportedSupported (fast lookup by key)
Index-based AccessSupported (by position)Not supported (no order guarantee)
Typical Use CasesOrdered lists, simple collectionsFast lookups, mapping unique keys to values
Program.cs

Program.cs

copy
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
using System; using System.Collections.Generic; namespace ConsoleApp { public class Program { public static void Main(string[] args) { // List of users (each user is a tuple of ID and Name) var usersList = new List<(int Id, string Name)> { (1, "Alice"), (2, "Bob"), (3, "Charlie"), }; // Search for user with ID 2 in the list string foundName = null; foreach (var user in usersList) { if (user.Id == 2) { foundName = user.Name; break; } } Console.WriteLine("List search found: " + foundName); // Dictionary of users (ID as key, Name as value) var usersDict = new Dictionary<int, string> { {1, "Alice"}, {2, "Bob"}, {3, "Charlie"}, }; // Search for user with ID 2 in the dictionary string dictName = null; if (usersDict.TryGetValue(2, out dictName)) { Console.WriteLine("Dictionary search found: " + dictName); } } } }

When you switch from a list to a dictionary for lookups, you gain significant speed improvements for large collections. However, dictionaries do use more memory because they must store keys, values, and maintain a hash table to enable fast access. If your data is small or you need to preserve order or allow duplicates, a list might be simpler and more memory-efficient. For large datasets where you need to find items by a unique identifier quickly, a dictionary is almost always the better choice.

When you switch from a list to a dictionary for lookups, you gain significant speed improvements for large collections. However, dictionaries do use more memory because they must store keys, values, and maintain a hash table to enable fast access. If your data is small or you need to preserve order or allow duplicates, a list might be simpler and more memory-efficient. For large datasets where you need to find items by a unique identifier quickly, a dictionary is almost always the better choice.

Program.cs

Program.cs

copy
123456789101112131415161718192021222324252627282930313233343536373839404142434445
using System; using System.Collections.Generic; namespace ConsoleApp { public class Program { public static void Main(string[] args) { // Original approach: searching a list for a product by ID var productsList = new List<(int Id, string Name)> { (101, "Laptop"), (102, "Phone"), (103, "Tablet"), }; string productName = null; foreach (var product in productsList) { if (product.Id == 102) { productName = product.Name; break; } } Console.WriteLine("List search: " + productName); // Refactored approach: using a dictionary for fast lookup var productsDict = new Dictionary<int, string> { {101, "Laptop"}, {102, "Phone"}, {103, "Tablet"}, }; string dictProductName = null; if (productsDict.TryGetValue(102, out dictProductName)) { Console.WriteLine("Dictionary lookup: " + dictProductName); } } } }

1. When is a dictionary more efficient than a list?

2. What is a drawback of using dictionaries?

question mark

When is a dictionary more efficient than a list?

Select the correct answer

question mark

What is a drawback of using dictionaries?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 6
some-alt