Click here to Skip to main content
15,935,248 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to replace the values of a key in a dictionary while iterating?
Posted
Updated 17-Jun-15 18:38pm
v2

 
Share this answer
 
You don't need to iterate to change the dictionary.
Try
C#
dict.Remove(keyToUpdate);
dict.Add(newKey, value);


You will not be able to change the collection while you are iterating through it.
A way out is to create a temporary list of key value pairs, iterate through this list and change the key as appropriate.
 
Share this answer
 
v2
See this post to help you,http://stackoverflow.com/questions/6499334/best-way-to-change-dictionary-key[^], you can't rename keys once that have been added to a Dictionary, first remove then add a new.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900