.NET Lookup Table – Is There a Key/Key Lookup Table Type in .NET?

cdictionarylistlookup

It seems like a pretty straightforward thing to add, but I just want to be sure .NET doesn't already provide one and save me from adding unnecessary code:

I need a lookup table (like a Dictionary) that instead using a key/value pair, uses the first key to find the second key, and vice versa. For example –

theList.Add("foo", "bar");
x = theList["foo"]; // Returns "bar"
y = theList["bar"]; // Returns "foo"

Thanks in advance!

Best Answer

There is no such datatype, probably because this is a very special requirement which can be easily solved by utilizing a dictionary and adding simply both pairs

 theDictionary.Add("foo", "bar");
 theDictionary.Add("bar", "foo");

Obviously, you can put this into a generic function like

void MyDictAdd(Dictionary<T,T> dict, T key1, T key2)
{
    dict.Add(key1,key2);
    dict.Add(key2,key1);
}
Related Topic