-1

Example I Using This Code but this code not changing value in the dictionary auto update value in dictionary on changed is possible ?

Dictionary<string, string> dic = new Dictionary<string, string>();
private void DoWork()
{
        dic.Add("data", "example1");
        dic.Add("data2", "example2");
        if(dic.TryGetValue("data",out string c))
        {
            c = "Value Changed Good";
        }

        foreach (string s in dic.Values)
        {
            Console.WriteLine(s);
        }
}

Example this code output is a

example1
example2

but value not changed example if value changed this output changed with this

Value Changed Good
example2
  • Does this answer your question? [How to update the value stored in Dictionary in C#?](https://stackoverflow.com/questions/1243717/how-to-update-the-value-stored-in-dictionary-in-c) – Sinatr Apr 14 '21 at 07:43
  • `if (dic.ContainsKey("data")) dic["data"] = "Value Changed Good";` – Dmitry Bychenko Apr 14 '21 at 07:46
  • @DmitryBychenko, that would be suboptimal, since it will require 2 look up, right? – Sinatr Apr 14 '21 at 07:47
  • 1
    @Sinatr: in case of ordinary `Dictionary` we don't have `UpdateIfExist` or alike and have to perform 2 looking up (it's not that expensive if we have a good hash). In case of `ConcurrentDictionary` we can do the modification in one go – Dmitry Bychenko Apr 14 '21 at 07:53

2 Answers2

3

If you change the value of your local variable c, this doesn't affect the value within the dictionary.

To update the value within the Dictionary you can do something like this:

var key = "data";
if(dic.TryGetValue(key,out string c))
{
    c = "Value Changed Good";
    dic[key] = c;
}
Oliver
  • 43,366
  • 8
  • 94
  • 151
  • Is this answer accurate for normal reference types? (yes, I know string is an exception) Edit: Accurate meaning, "this doesn't affect the value within the dictionary". I believe reference types are mutable. – John Apr 14 '21 at 07:47
  • @JohnD This is accurate for all reference types. If you assign a new value to `c`, this means `c` points to a new address in memory. The variable stored in the dictionary will still point to the old address in memory. – SomeBody Apr 14 '21 at 08:03
  • Ty, I didn't account for the out param being a new local var. – John Apr 14 '21 at 08:10
1

If you want to update collection items, you should probably use the solution provided in Oliver's answer. That's the most straightforward way to go, I guess.

If you want to avoid a duplicate lookup when updating collection values in your for-loop, you could consider wrapping your collection values in a wrapper object. It could look something like this:

class Wrapper
{
    public string Value { get; set; }
}

Dictionary<string, Wrapper> dic = new Dictionary<string, Wrapper>();

private void DoWork()
{
    dic.Add("data", new Wrapper { Value = "example1" });
    dic.Add("data2", new Wrapper { Value = "example2" });

    if (dic.TryGetValue("data", out Wrapper c))
    {
        c.Value = "Value Changed Good";
    }

    foreach (Wrapper s in dic.Values)
    {
        Console.WriteLine(s.Value);
    }
}

But mind that this will also introduce additional overhead (especially when reading values from the dictionary). And it will probably make your code somewhat harder to read and understand as well.

Bart Hofland
  • 3,700
  • 1
  • 13
  • 22