Loop in the dictionary

I use this:

foreach(KeyValuePair<String,String> entry in MyDic) { // do something with entry.Value or entry.Key } 

The problem is that I cannot change the value of entry.Value or entry.Key

My question is, how can I change the value or key while passing through the dictionary? And does the dictionary allow duplicate keys? And if so, how can we avoid it? Thanks you

+3
dictionary c #
Jun 29 '11 at 3:18
source share
5 answers

You cannot change the value of a dictionary during a loop through elements in a dictionary, although you can change a property to a value if it is an instance of a reference type.

For example,

 public class MyClass { public int SomeNumber { get; set;} } foreach(KeyValuePair<string, MyClass> entry in myDict) { entry.Value.SomeNumber = 3; // is okay myDict[entry.Key] = new MyClass(); // is not okay } 

Attempting to modify a dictionary (or any collection) while passing through its elements will cause an InvalidOperationException report that the collection has been modified.

To answer your specific questions,

My question is, how can I change the value or key while passing through the dictionary?

The approach to both will be almost the same. You can either iterate over a copy of the dictionary, as Anthony Pengram said in his answer , or you can skip through all the elements once to find out which ones you need to change and then scroll through the list of these elements again:

 List<string> keysToChange = new List<string>(); foreach(KeyValuePair<string, string> entry in myDict) { if(...) // some check to see if it an item you want to act on { keysToChange.Add(entry.Key); } } foreach(string key in keysToChange) { myDict[key] = "new value"; // or "rename" a key myDict["new key"] = myDict[key]; myDict.Remove(key); } 

And, can a dictionary allow a duplicate key? And if so, how can we avoid it?

The dictionary does not allow duplicate keys. If you need a collection of <string, string> pairs, run NameValueCollection .

+7
Jun 29 '11 at 3:32
source share

Updating the dictionary in a loop will be a problem, since you cannot change the dictionary because it is listed. However, you can easily get around this by translating the dictionary into a list of KeyValuePair<> objects. You list this list, and then you can change the dictionary.

 foreach (var pair in dictionary.ToList()) { // to update the value dictionary[pair.Key] = "Some New Value"; // or to change the key => remove it and add something new dictionary.Remove(pair.Key); dictionary.Add("Some New Key", pair.Value); } 

For the second part, the key in the dictionary must be unique.

+6
Jun 29 '11 at 3:27
source share

KeyValuePair The key and value is read-only. But you can change this value:

 dictionary[key].Value = newValue; 

But if you want to change the key, you will have to delete / add the key.

And no, the dictionary does not allow duplicate keys, it will throw an ArgumentException.

0
Jun 29 '11 at 3:21
source share

You cannot change keys when listing them.

One method that I use to make changes to the collection when enumerating them is to break; from the foreach , when a match is found and the element is changed, and again I restart all the enumeration. This is one way to handle it ...

No, the dictionary cannot have duplicate keys. If you want something that will sort by key and allow duplicates, you must use a different data structure.

0
Jun 29 2018-11-11T00:
source share

You can do it like

 for (int i = 0; i < MyDic.Count; i++) { KeyValuePair<string, string> s = MyDic.ElementAt(i); MyDic.Remove(s.Key); MyDic.Add(s.Key, "NewValue"); } 

And the dictionary does not allow duplication

-2
Jun 29
source share



All Articles