Skip to content

How To Modify An Element in the Dictionary Class?

William Thompson

William Thompson

Asked Aug 18, 2012, 7:45 PM EDT3 answers
Share
LinkedInX
How To Modify An Element in the Dictionary Class?
===============================
C# has this cool Dictionary class that you can use like a Hash Table.  Is there a way of changing the value of an indexed element without resorting to removing it like this?
  int value = runningcount[city];
  runningcount.Remove(city);
  runningcount.Add(city, ++value);

and, by the way, how do I change my avatar here?

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Akkiraju Ivaturi
    0
    Somewhere I saw this, I am not sure if it helps:

    if the dictionary is declared as Dictionary list = new Dictionary();

    then you can set the value for the key "test" as follows

    list["test"] = list["test"] + 1;

    now the value for the key will get incremented by 1.


    But, do let me know if you can change your avatar... Even I am facing the same challenge... :) Thanks...

    0
  • William Thompson
    0
    thanks. I am trying your suggestion now.
    0
  • Akkiraju Ivaturi
    0
    Please mark it as accepted, if you are satisfied with the answer. Thanks.
    0