Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use dictionary two way in c#

Tags:

c#

public static Dictionary<int, string> dic = new Dictionary<int, string>() { 
            {1,"anystring1"},
            {2,"anystring2"}};

I need to use this

string str= dic[1]; // it is possible

int a=dic["anystring1"]; // My dream is it
like image 544
ebattulga Avatar asked Nov 16 '10 05:11

ebattulga


People also ask

Can Dictionary have duplicate keys C#?

In Dictionary, key must be unique. Duplicate keys are not allowed if you try to use duplicate key then compiler will throw an exception. In Dictionary, you can only store same types of elements.

Where to use Dictionary in C#?

You use Dictionary<TKey,TValue> when you need to store values with some unique keys associated to them, and accessing them by that key is convenient for you.

What happens if we add duplicate key in Dictionary C#?

Its not possible to add duplicate items to a Dictionary - an alternative is to use the Lookup class. Creates a generic Lookup from an IEnumerable.

Do dictionaries exist in C?

Generally, the C standard library does not include a built-in dictionary data structure, but the POSIX standard specifies hash table management routines that can be utilized to implement dictionary functionality.


1 Answers

Use another Dictionary<> and use it in reverse order of key/value.

like image 85
Daniel Mošmondor Avatar answered Sep 19 '22 15:09

Daniel Mošmondor