How to iterate through Dictionary and change values?

According to MSDN:

The foreach statement is a wrapper
around the enumerator, which allows
only reading from the collection, not
writing to it.

Use this:

var dictionary = new Dictionary<string, double>();
// TODO Populate your dictionary here
var keys = new List<string>(dictionary.Keys);
foreach (string key in keys)
{
   dictionary[key] = Math.Round(dictionary[key], 3);
}

Leave a Comment