Are the keys in a Dictionary<TKey, TValue> immutable?

.net, c#, collections, generics

Solution

You are right, if the key changes, you will need to remove the entry with the old key and insert a new entry with the new key.

If only some of the keys change, you don't need to copy the entire dictionary. Both Add and Remove operations on `Dictionary` are roughly O(1) operations.

Problem

I get a `Dictionary<string, string>` back from an API but depending on certain rules need to modify some of the key names. Before I head down the road of copying to a new `Dictionary` I just wanted to confirm that the keys in a `Dictionary<TKey, TValue>` are immutable.

Original source

Related problems