Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a Dictionary in C#?
A Dictionary in C# is a collection that stores data as key-value pairs, where each key is unique and is used to access its corresponding value quickly.
Click to reveal answer
beginner
How do you add an item to a Dictionary in C#?
Use the Add(key, value) method or the indexer syntax dictionary[key] = value; to add a new key-value pair.
Click to reveal answer
intermediate
What happens if you try to add a duplicate key to a Dictionary?
Adding a duplicate key causes an exception because keys must be unique in a Dictionary.
Click to reveal answer
beginner
How can you check if a key exists in a Dictionary?
Use the ContainsKey(key) method which returns true if the key exists, otherwise false.
Click to reveal answer
intermediate
How do you retrieve a value safely from a Dictionary without risking an exception?
Use TryGetValue(key, out value) which returns true if the key exists and outputs the value; otherwise, it returns false.
Click to reveal answer
What type of data structure is a Dictionary in C#?
AKey-value collection
BList of values
CArray of keys
DStack
✗ Incorrect
A Dictionary stores data as key-value pairs, making it a key-value collection.
Which method checks if a key exists in a Dictionary?
AContainsKey()
BExists()
CContainsValue()
DFindKey()
✗ Incorrect
ContainsKey() checks if a specific key exists in the Dictionary.
What happens if you add a key that already exists in a Dictionary using Add()?
AThe Dictionary clears all items
BThe value is updated
CThe key is ignored
DAn exception is thrown
✗ Incorrect
Adding a duplicate key with Add() throws an exception because keys must be unique.
Which method safely retrieves a value from a Dictionary?
AFetchValue()
BGetValue()
CTryGetValue()
DRetrieve()
✗ Incorrect
TryGetValue() safely retrieves a value and avoids exceptions if the key is missing.
How do you add a key-value pair to a Dictionary using indexer syntax?
Adictionary.Add(key, value);
Bdictionary[key] = value;
Cdictionary.Insert(key, value);
Ddictionary.Set(key, value);
✗ Incorrect
Using dictionary[key] = value; adds or updates the key-value pair.
Explain what a Dictionary key-value collection is and how it works in C#.
Think about how you use a real dictionary to find a word's meaning.
You got /4 concepts.
Describe how to safely retrieve a value from a Dictionary without causing an error if the key does not exist.
It's like checking if a word exists before reading its meaning.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a Dictionary in C#?
easy
A. To create graphical user interfaces
B. To store data in a sequential list
C. To perform mathematical calculations
D. To store data as key-value pairs for quick access
Solution
Step 1: Understand the Dictionary concept
A Dictionary stores data as pairs where each key is linked to a value for fast lookup.
Step 2: Compare with other options
Options B, C, and D describe lists, math, and UI, which are unrelated to Dictionary's purpose.
Final Answer:
To store data as key-value pairs for quick access -> Option D
Quick Check:
Dictionary = key-value pairs [OK]
Hint: Remember: Dictionary = keys + values for fast lookup [OK]
Common Mistakes:
Confusing Dictionary with List
Thinking Dictionary stores only values
Assuming Dictionary is for UI or math
2. Which of the following is the correct way to add a key-value pair to a Dictionary<int, string> named dict?
easy
A. dict.Add(1, "apple");
B. dict.Insert(1, "apple");
C. dict["1"] = "apple";
D. dict.Put(1, "apple");
Solution
Step 1: Recall Dictionary methods
The method to add a new key-value pair is Add(key, value).
Step 2: Check options
Only dict.Add(1, "apple"); uses Add. dict[1] = "apple"; uses indexer which sets or updates but is not the method to add explicitly.
Final Answer:
dict.Add(1, "apple"); -> Option A
Quick Check:
Add() method adds key-value pair [OK]
Hint: Use Add() to insert new pairs, indexer to update [OK]
Common Mistakes:
Using Insert or Put which don't exist
Confusing Add() with indexer for adding
Trying to add with wrong method name
3. What will be the output of this C# code?
var dict = new Dictionary<int, string>();
dict.Add(1, "one");
dict[2] = "two";
Console.WriteLine(dict[1] + ", " + dict[2]);
medium
A. two, one
B. 1, 2
C. one, two
D. Runtime error
Solution
Step 1: Understand dictionary additions
Key 1 is added with value "one" using Add(). Key 2 is set to "two" using indexer.
Step 2: Check output of Console.WriteLine
It prints values for keys 1 and 2 joined by comma: "one, two".
Final Answer:
one, two -> Option C
Quick Check:
dict[1] = one, dict[2] = two [OK]
Hint: Indexer sets or updates; Add inserts new pairs [OK]
Common Mistakes:
Confusing keys with values in output
Expecting keys printed instead of values
Thinking indexer causes error if key missing
4. What is wrong with this code snippet?
var dict = new Dictionary<int, string>();
dict.Add(1, "apple");
dict.Add(1, "banana");
medium
A. It will compile and run without errors
B. It causes a runtime exception due to duplicate key
C. It overwrites the first value with the second
D. It causes a compile-time error
Solution
Step 1: Understand Add() behavior with duplicate keys
Adding a key that already exists causes a runtime exception.
Step 2: Check code behavior
Second Add with key 1 throws an ArgumentException at runtime.
Final Answer:
It causes a runtime exception due to duplicate key -> Option B
Quick Check:
Duplicate keys cause runtime error [OK]
Hint: Add() fails if key exists; use indexer to overwrite [OK]
Common Mistakes:
Assuming Add overwrites existing key
Expecting compile-time error instead of runtime
Confusing Add with indexer behavior
5. Given a list of user IDs and names:
var users = new List<(int id, string name)> { (1, "Alice"), (2, "Bob"), (3, "Charlie") };
Which code correctly creates a Dictionary<int, string> mapping IDs to names using a dictionary comprehension style?
hard
A. var dict = users.ToDictionary(u => u.id, u => u.name);
B. var dict = new Dictionary<int, string>(); foreach(var u in users) dict.Add(u.name, u.id);
C. var dict = users.Select(u => new { u.id, u.name }).ToList();
D. var dict = new Dictionary<string, int>(); foreach(var u in users) dict[u.id] = u.name;
Solution
Step 1: Understand ToDictionary usage
ToDictionary converts a list to a dictionary by specifying key and value selectors.
Step 2: Check each option
var dict = users.ToDictionary(u => u.id, u => u.name); correctly maps id to name. var dict = new Dictionary<int, string>(); foreach(var u in users) dict.Add(u.name, u.id); swaps key and value and uses wrong types. var dict = users.Select(u => new { u.id, u.name }).ToList(); creates a list, not dictionary. var dict = new Dictionary<string, int>(); foreach(var u in users) dict[u.id] = u.name; has wrong dictionary types and indexer usage.
Final Answer:
var dict = users.ToDictionary(u => u.id, u => u.name); -> Option A
Quick Check:
ToDictionary creates dictionary from list [OK]
Hint: Use ToDictionary with key and value selectors [OK]