You can either ToList over the dictionary (you need to use ToList because you cannot change the collection that loops in the foreach loop)
foreach(var key in dict.Keys.ToList()) { dict[key] = "Hello"; }
or you can do this on a single LINQ line, since you are setting the same value for all keys.
dict = dict.ToDictionary(x => x.Key, x => "Hello");
Updated Question
foreach (var key in dict.Keys.ToList()) { dict[key] = key + dict[key]; }
and LINQ version
dict = dict.ToDictionary(x => x.Key, x => x.Key + x.Value);
If you want to avoid using ToList , you can use ElementAt so that you can directly modify the collection.
for (int i = 0; i < dict.Count; i++) { var item = dict.ElementAt(i); dict[item.Key] = item.Key + item.Value; }
source share