Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update the value stored in Dictionary in C#?

Tags:

c#

dictionary

How to update value for a specific key in a dictionary Dictionary<string, int>?

like image 833
Amit Avatar asked Aug 07 '09 09:08

Amit


2 Answers

Just point to the dictionary at given key and assign a new value:

myDictionary[myKey] = myNewValue;
like image 145
ccalboni Avatar answered Oct 16 '22 19:10

ccalboni


It's possible by accessing the key as index

for example:

Dictionary<string, int> dictionary = new Dictionary<string, int>();
dictionary["test"] = 1;
dictionary["test"] += 1;
Console.WriteLine (dictionary["test"]); // will print 2
like image 20
Amit Avatar answered Oct 16 '22 19:10

Amit