how to access a dictionary item by key, if the key is

List<string>? 

I apologize for the incomplete description. I need not just get the value, but increase it by 1.

  • List is not a dictionary, but a listSublihim
  • List<T> should not be used as a hash table key - it does not have GetHashCode redefined. - kmv

2 answers 2

  1. Just by "index", with exception handling KeyNotFoundException
 try { Console.WriteLine("Значение = {0}.", dict[key]); } catch (KeyNotFoundException) { Console.WriteLine("Ключ не найден."); } 
  1. Dictionary<TKey, TValue>.TryGetValue (TKey, TValue) method Dictionary<TKey, TValue>.TryGetValue (TKey, TValue)
    msdn: TryGetValue
 if (dict.TryGetValue(key, out value)) { Console.WriteLine("Значение = {0}.", value); } else { Console.WriteLine("Ключ не найден."); } 
  1. Check the availability of the key
    msdn: ContainsKey
 if (dict.ContainsKey(key)) { Console.WriteLine("Значение = {0}.", dict[key]); } else { Console.WriteLine("Ключ не найден."); } 

    Use the Dictionary.TryGetValue() method

     var myValue; List<string> key; if (MyDict.TryGetValue(key, out myValue)) { /* use myValue */ }