Increase dictionary value by specified key

I have a bool variable and a dictionary. if this is true, I need to increase the value of my dictionary by 1 for a given key.

My code is:

 private void Process(Person person)
 {
     isSendMailSuccessful = true;

     if (isSendMailSuccessful)
     {
         MyDictionary.Where(i => i.Key == person.personID);
         // I need to increase Value of that ID by 1
     }
 }
+3
source share
5 answers

Why are you using LINQ for this, and not just for a dictionary indexer?

Sort of

MyDictionary[person.personID] += 1;
+6
source

There is no reason to use LINQ here.

Assuming your dictionary Dictionary<int,int>, you can just do myDictionary[person.PersonID]++;.

You must use ContainsKeyit first to make sure the key exists in the dictionary, otherwise it will throw an exception if you try to change a dictionary entry that does not exist.

+4
source
MyDictionary[person.personID] += 1;

ContainsKey, , .

+2

- :

MyDictionary[person.personID] += 1;
+1
 private void Process(Person person)
 {
     isSendMailSuccessful = true;

     if (isSendMailSuccessful)
     {
         MyDictionary[person.personID] += 1;
     }
 }

, , ++, += 1 , : MyDictionary[person.personID] = MyDictionary[person.personID] + 1;

+1
source

All Articles