24

I am storing a string and int value in Key value pair.

var list = new List<KeyValuePair<string, int>>();

While adding i need to check if string(Key) already exists in list, if exists i need to add it to Value instead of adding new key.
How to check and add?

leppie
  • 115,091
  • 17
  • 196
  • 297
Olivarsham
  • 1,701
  • 5
  • 25
  • 51

6 Answers6

36

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;
Habib
  • 219,104
  • 29
  • 407
  • 436
7

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;
Ravi Gadag
  • 15,735
  • 5
  • 57
  • 83
7

For anyone who has to use a List (which was the case for me, since it does things Dictionary doesn't), you can just use a lambda expression to see if the List contains the Key:

list.Any(l => l.Key == checkForKey);
WATYF
  • 409
  • 1
  • 5
  • 16
5

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

Jhonny
  • 61
  • 1
4

Your needs exactly describe the design of Dictionarys?

Dictionary<string, string> openWith = 
        new Dictionary<string, string>();

// Add some elements to the dictionary. There are no  
// duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe");

// If a key does not exist, setting the indexer for that key 
// adds a new key/value pair.
openWith["doc"] = "winword.exe";
Karthik T
  • 31,456
  • 5
  • 68
  • 87
4

For sure, dictionary is preferable in your case. You can not modify the Value of KeyValue<string,int> class as it is Immutable.

But even if you still want to use List<KeyValuePair<string, int>>();. You can use IEqualityComparer<KeyValuePair<string, int>>. Code will be like.

public class KeyComparer : IEqualityComparer<KeyValuePair<string, int>>
{

    public bool Equals(KeyValuePair<string, int> x, KeyValuePair<string, int> y)
    {
        return x.Key.Equals(y.Key);
    }

    public int GetHashCode(KeyValuePair<string, int> obj)
    {
        return obj.Key.GetHashCode();
    }
}

And use it in Contains like

var list = new List<KeyValuePair<string, int>>();
        string checkKey = "my string";
        if (list.Contains(new KeyValuePair<string, int>(checkKey, int.MinValue), new KeyComparer()))
        {
            KeyValuePair<string, int> item = list.Find((lItem) => lItem.Key.Equals(checkKey));
            list.Remove(item);
            list.Add(new KeyValuePair<string, int>("checkKey", int.MinValue));// add new value
        }

which does not sounds good way.

hope this info helps..

D J
  • 6,908
  • 13
  • 43
  • 75