Strange container behaviour?

G

Guest

When I try to run this following small code sample:

using System;
using System.Collections.Generic;

class Program {
static void Main(string[] args) {
Dictionary<string, int> MyDict = new Dictionary<string, int>();
MyDict["aaa"] = 0;
MyDict["aba"] = 1;
MyDict["abb"] = 2;
MyDict["baa"] = 3;

foreach (string s in MyDict.Keys) {
MyDict += 5;
Console.WriteLine(MyDict);
}
}
}

I get the exception: Collection was modified; enumeration operation may not
execute.

But I do not modify the key!
How can I "edit" values after adding them to an dictionary?

I can use the following loop to enumerate all entries:

foreach (string s in new List<string>(MyDict.Keys)) {

but this is very inefficient if I have really large dictionaries and/or
large keys.

Thanks for any help
Juergen
 
A

Andy

When I try to run this following small code sample:

using System;
using System.Collections.Generic;

class Program {
static void Main(string[] args) {
Dictionary<string, int> MyDict = new Dictionary<string, int>();
MyDict["aaa"] = 0;
MyDict["aba"] = 1;
MyDict["abb"] = 2;
MyDict["baa"] = 3;

foreach (string s in MyDict.Keys) {
MyDict += 5;
Console.WriteLine(MyDict);
}
}

}

I get the exception: Collection was modified; enumeration operation may not
execute.

But I do not modify the key!
How can I "edit" values after adding them to an dictionary?

I can use the following loop to enumerate all entries:

foreach (string s in new List<string>(MyDict.Keys)) {

but this is very inefficient if I have really large dictionaries and/or
large keys.

Thanks for any help
Juergen


That is by design; you can't change the list in a foreach operation.

What's happening is that you're storing a value type, and the addition
operation produces a new integer object.
 
G

Guest

Andy said:
That is by design; you can't change the list in a foreach operation.

What's happening is that you're storing a value type, and the addition
operation produces a new integer object.


But how can I modify all values in a dictionary?
Enumerating through a copy of the keys can't be the solution.
In C++ i can simply write:

#include "stdafx.h"
#include <iostream>
#include <string>
#include <map>

using namespace std;

void _tmain()
{
map<string, int> MyMap;
MyMap["aaa"] = 0;
MyMap["aab"] = 1;
MyMap["aba"] = 2;
MyMap["baa"] = 3;

map<string, int>::iterator i = MyMap.begin();
for (i = MyMap.begin(); i != MyMap.end(); i++) {
i->second += 5;
cout << i->second << endl;
}
}

How can I loop through a dictionary in C# without using a foreach?

Regards,
Juergen
 
G

Guest

Hmm, thanks for your hint, but how can I enumerate over the Keys collection
with a for loop?

If I use something like:

for (int i = 0; i < MyDict.Values.Count; i++)

I have no idea how I can use the index i together with the Keys- or
ValuesCollection. (MyDict.Values does not work).

And any foreach combination like:
foreach (string s in MyDict.Keys)
or
foreach (KeyValuePair<string, int> kvp in MyDict)
or
foreach (int n in MyDict.Values)

cannot work

Is there any other way to use a for loop with the collection?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top