In C#, you update a value in a Dictionary by assigning to an existing key with the indexer — it overwrites the old value:
Dictionary<string, int> scores = new Dictionary<string, int>();
scores["Alice"] = 95;
scores["Bob"] = 87;
scores["Alice"] = 99; // update: Alice is now 99
scores["Charlie"] = 72; // add new entry
scores[key] = value is used for both inserting and updating. To update only existing keys, check with ContainsKey() first:
if (scores.ContainsKey("Alice"))
{
scores["Alice"] = 99; // only updates if key exists
}
Assignment
Follow the Coding Tutorial and update Alice's score to 99!
Hint
Use scores["Alice"] = 99.
Free plan: 30 tutor messages. PRO is unlimited.
Saved
Ctrl + Enter to check
