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
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
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