how to change Dictionary's value when enumerate it?

Posted by younevertell on Stack Overflow See other posts from Stack Overflow or by younevertell
Published on 2010-04-23T06:02:32Z Indexed on 2010/04/23 6:03 UTC
Read the original article Hit count: 206

Filed under:

how to change Dictionary's value when enumerate it? the following code doesn't work, because we can not change dictionary's value when enumerating it. Is there any way to get around it? Or NO WAY? Thanks

foreach (KeyValuePair<string, int> kvp in mydictionary)
        {
            if (otherdictionary.ContainsKey(kvp.Key))
            {
                mydictionary[kvp.Key] = otherdictionary[kvp.Key];

            }
            else
            {
                otherdictionary[kvp.Key] = mydictionary[kvp.Key];
            }
        }

© Stack Overflow or respective owner

Related posts about c#