Finding already existing value in Key Value pair

use dictonary. Dictionary in C# and I suggest you to read this post Dictonary in .net

Dictionary<string, int> dictionary =
        new Dictionary<string, int>();
    dictionary.Add("cat", 2);
    dictionary.Add("dog", 1);
    dictionary.Add("llama", 0);
    dictionary.Add("iguana", -1);

to check. use ContainsKey ContainsKey

if (dictionary.ContainsKey("key"))
    dictionary["key"] = dictionary["key"] + yourValue;

Instead of List you can use Dictionary and check if it contains key then add the new value to the existing key

int newValue = 10;
Dictionary<string, int> dictionary = new Dictionary<string, int>();
if (dictionary.ContainsKey("key"))
    dictionary["key"] = dictionary["key"] + newValue;

If you need use the list,you must foreach the list,and look for the keys. Simplely,you can use hashtable.

Tags:

C#

.Net

Key Value