in

C#: what is the efficient way to change the value for a key in System.Collections.Generic.Dictionary?

Hi Experts:

    With C# I declared a System.Collections.Generic.Dictionary variable and want to update the value for some key. I couldn't find a method directly update the value, and I end up doing a remove_key-->add_key as the following.
    Is there any way to write shorter or faster code to do the same job?
    Thank you!

Sui
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
if (Dict.TryGetValue(strKey out intVal))
{
        intVal += 100;
        CardDict.Remove(strKey);
        CardDict.Add(strKey, intVal);
}
else
{
         CardDict.Add(strKey, 100);
}
Open in New Window Select All

Solution: C#: what is the efficient way to change the value for a key in System.Collections.Generic.Dictionary?

1:
CardDict[strKey] = intVal;
Open in New Window Select All