0
0
C Sharp (C#)programming~5 mins

Dictionary key-value collection in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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
Which method checks if a key exists in a Dictionary?
AContainsKey()
BExists()
CContainsValue()
DFindKey()
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
Which method safely retrieves a value from a Dictionary?
AFetchValue()
BGetValue()
CTryGetValue()
DRetrieve()
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);
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.