I have the following code in c# , basically its a simple dictionary
with some keys and their values . Now i want to update the existing key in
this dictionary with new key. Is it possible?
Dictionary<string, int> dictionary = new Dictionary<string, int>();
dictionary.Add("00", 2);
dictionary.Add("01", 1);
dictionary.Add("02", 0);
dictionary.Add("03", -1);
In certain conditions I removed the value with key 01.
Now I have to update the key below the deleted key like below.
dictionary.Add("00", 2);
dictionary.Add("01", 0);
dictionary.Add("02", -1);
Is it possible?
That suggests you are using a dictionary entirely incorrectly. A dictionary is a mapping between keys and values. If we erase a word from “the” dictionary (the thing with words), the definitions don’t all move by one word!
To do what you want, just use a list, and use the index in place of how you are currently using the key:
now the list is {index, value}:
{0, 2}, {1, 0}, {2, -1}