By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Generic collections, specifically List and Dictionary, are fundamental data structures in C#. They allow you to store and manage groups of objects efficiently. Mastering these collections is crucial for writing clean, maintainable, and high-performance code. Incorrect usage can lead to inefficient algorithms, memory leaks, and hard-to-debug errors. For instance, improperly handling a Dictionary can result in key collisions, leading to data loss or corruption.
List<int> numbers = new List<int>();
⚠️ Pitfall: Forgetting to initialize the list can lead to null reference exceptions.
Add Elements to List
Add
numbers.Add(1); numbers.Add(2);
⚠️ Pitfall: Adding elements in a loop without checking for duplicates can lead to redundant data.
Access Elements in List
int firstNumber = numbers[0];
⚠️ Pitfall: Accessing an index out of range will throw an exception.
Initialize a Dictionary
Dictionary<string, int> ages = new Dictionary<string, int>();
⚠️ Pitfall: Using non-unique keys will overwrite existing values.
Add Elements to Dictionary
ages["Alice"] = 30; ages.Add("Bob", 25);
⚠️ Pitfall: Adding a key that already exists will throw an exception.
Access Elements in Dictionary
int aliceAge = ages["Alice"];
Experts view List and Dictionary as tools for different tasks. List is ideal for ordered collections where position matters, while Dictionary is perfect for quick lookups based on unique keys. They think in terms of performance characteristics and choose the collection that best fits the problem at hand.
Exam trap: Questions that require efficient data retrieval.
The mistake: Not checking for key existence in Dictionary.
ContainsKey
TryGetValue
Exam trap: Scenarios involving key-based data retrieval.
The mistake: Adding duplicate keys to Dictionary.
Exam trap: Questions about data integrity.
The mistake: Accessing out-of-range indices in List.
IndexOutOfRangeException
Scenario: You need to store and retrieve student grades by their IDs.Question: Which collection should you use and why? Solution: Use Dictionary because it allows quick retrieval of grades based on unique student IDs.Answer: DictionaryWhy it works: Dictionary provides O(1) lookup time, making it efficient for retrieving grades.
Scenario: You need to maintain a list of tasks to be completed in order.Question: Which collection should you use and why? Solution: Use List because it maintains the order of elements.Answer: ListWhy it works: List allows you to access tasks by their position, maintaining the order of completion.
Scenario: You need to store employee salaries and retrieve them by employee name.Question: Which collection should you use and why? Solution: Use Dictionary because it allows quick retrieval of salaries based on unique employee names.Answer: DictionaryWhy it works: Dictionary provides efficient lookup by key, making it ideal for retrieving salaries.
List<T>
Dictionary<TKey, TValue>
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.