I declared the dictionary obj.
Dictionary<string, string> aDict = new Dictionary<string, string>();
aDict .Add("IP", "Host");
As I remembered,
The expression of aDict[IP] can return the value (Host).
Now if I go in the opposite direction.
How to get the Key from Value ? aDict[Host] ?
Does the Dictionary is a one-way street in C#, only running from Key to Value ? thanks.
Dictionaries do not work like this, nor are they intended to. How would you resolve the following:
You could do this:
But if you really need to go back and forth between keys and values you should consider encapsulating the problem into a two-way map. Here’s a very simple implementation of this that you can tailor to your needs:
Note that this assumes that no key is ever duplicated.
Now you can say: