Very often I use Dictionary<TKey, TValue> type to store ehh…dictionary type of values, e.g. Key = 1, Value ="Canada". But in many cases, values in the dictionary data is also unique, just like the Keys. And I find that for Dictionary<TKey, TValue> type, it is not very convenient to get key value based on value.
My question is, which class in .NET, is suitable in this scenario? Quickly find out Value based on Key and vice versa.
This is pretty trivial using LINQ:
And an extension method if you fancy:
What you can also do is to implement
IDictionary, delegate to twoDictionaryobjects (one for key-value mapping and one for value-key mapping) for backing store and have a constraint onAddthat values must be unique. That way you are guaranteed that there would be at most one entry with a particular value. EDIT Using two dictionaries will improve access time(Thanks @SamStephens) as both keys and values will be stored in an ordered hashtable.