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#?
✗ 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?
✗ 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()?
✗ Incorrect
Adding a duplicate key with Add() throws an exception because keys must be unique.
Which method safely retrieves a value from a Dictionary?
✗ 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?
✗ 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.