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
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.
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.
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.
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.
Use another Dictionary<>
and use it in reverse order of key/value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With