Dictionaries 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:
| Feature | List | Dictionary<TKey, TValue> |
|---|---|---|
| Lookup Speed | O(n) — must check each item | O(1) — direct access by key |
| Memory Usage | Lower (stores only values) | Higher (stores keys and values, plus hashing overhead) |
| Duplicate Values | Allowed | Values allowed, but keys must be unique |
| Key-based Access | Not supported | Supported (fast lookup by key) |
| Index-based Access | Supported (by position) | Not supported (no order guarantee) |
| Typical Use Cases | Ordered lists, simple collections | Fast lookups, mapping unique keys to values |
Program.cs
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647using 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445using 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?
Obrigado pelo seu feedback!
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo
Incrível!
Completion taxa melhorada para 4.76
Dictionaries vs. Lists: Choosing the Right Tool
Deslize para mostrar o 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:
| Feature | List | Dictionary<TKey, TValue> |
|---|---|---|
| Lookup Speed | O(n) — must check each item | O(1) — direct access by key |
| Memory Usage | Lower (stores only values) | Higher (stores keys and values, plus hashing overhead) |
| Duplicate Values | Allowed | Values allowed, but keys must be unique |
| Key-based Access | Not supported | Supported (fast lookup by key) |
| Index-based Access | Supported (by position) | Not supported (no order guarantee) |
| Typical Use Cases | Ordered lists, simple collections | Fast lookups, mapping unique keys to values |
Program.cs
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647using 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445using 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?
Obrigado pelo seu feedback!