how to change the value of dictionary during looping

.net, asp.net, c#, collections

Solution

The string Replace function returns a new modified string, so you'd have to do something like:

foreach (var key in dtParams.Keys.ToArray())
{
   dtParams[key] = dtParams[key].Replace("'", "''");
}

EDIT:

Addressed the collection is modified issue (which I didn't think would occur if you access a key that already exists...)

Problem

How do I modify a value in Dictionary? I want to reassign a value to a value in my dictionary while looping on my dictionary like this: ``` for (int i = 0; i < dtParams.Count; i++) { dtParams.Values.ElementAt(i).Replace("'", "''"); } ``` where `dtParams` is my `Dictionary` I want to do some thing like this: ``` string a = "car"; a = a.Replace("r","t"); ```

Original source

Related problems