Two-way KeyValuePair collection in C #

By creating a Dictionary<int,int> or List<KeyValuePair<int,int>> , I can create a list of related identifiers.

By calling collection[key] , I can return the value corresponding to it.

I also want to be able to return the key by passing a value - which, as I know, is possible using some LINQ , however this does not seem very efficient.

In my case, along with each unique key, each value also . Does this fact mean using a different approach that will provide better performance?

+4
source share
2 answers

It looks like you need a bi-directional dictionary. There are no infrastructure classes for this, but you can implement your own:

1 on 1 Bidirectional Dictionary in C #

+3
source

You can encapsulate two dictionaries, one with your "keys" storing your values, and the other with your "values" storing your keys.

Then control access to them using several methods. Fast and superfluous overhead memory should not matter much.

Edit: just noticed that this is essentially the same as the previous answer: - /

0
source

Source: https://habr.com/ru/post/1432697/


All Articles