Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About Dictionary

Tags:

c#

dictionary

Can I make sure that in dictionary in C# there will be only a single of a specific value in it?

For example, if I define a Dictionary which key is char and value is char, can I make sure that if the character 'a' is already an existing value, there won't be another value 'a' in the dictionary?

I have a solution, but I'd like to know if there's a better one:

static void Main()
{
    Dictionary<int, int> dic = new Dictionary<int, int>();
    bool valExists = false;
    Console.WriteLine("please enter key and value");
    int key = int.Parse(Console.ReadLine());
    int val = int.Parse(Console.ReadLine());
    foreach (KeyValuePair<int,int> keyval in dic)
    {
        if (keyval.Value == val)
        {
            valExists = true;
            break;
        }
    }
    if (!valExists)
        dic.Add(key, val);
}
like image 681
user3376313 Avatar asked Mar 03 '14 19:03

user3376313


People also ask

What is the importance of dictionary?

A dictionary is one of the most important tools during your time studying at a university. A good dictionary can help you understand your subject better, improve your communication and improve your grades by making sure you are using words correctly.

What is dictionary and its uses?

A dictionary can be used to look up the meaning of a word. You can also use a dictionary to check the spelling of a word. Dictionaries may also give other information about words, such as word type and word origin.

What is meant by the dictionary?

Definition of dictionary 1 : a reference source in print or electronic form containing words usually alphabetically arranged along with information about their forms, pronunciations, functions, etymologies, meanings, and syntactic and idiomatic uses.

What is the English dictionary definition of about?

Define about. about synonyms, about pronunciation, about translation, English dictionary definition of about. adv. 1. Approximately; nearly: The interview lasted about an hour. 2. Almost: The job is about done. 3. To a reversed position or direction: Turn about and...

What is the purpose of the website dictionary com?

Dictionary.com strives to enable and inspire connection, communication, learning, creativity, and expression in a world powered by words. Dictionary.com is the world’s leading digital dictionary. We provide millions of English definitions, spellings, audio pronunciations, example sentences, and word origins.

What is the source of Your Dictionary?

Dictionary.com’s main, proprietary source is the Random House Unabridged Dictionary, which is continually updated by our team of experienced lexicographers and supplemented with trusted, established sources including American Heritage and Harper Collins to support a range of language needs.

Is Cambridge Dictionary free to use?

Cambridge University Press has been publishing dictionaries for learners of English since 1995. Cambridge Dictionaries Online began offering these dictionaries completely free of charge in 1999 — and today, Cambridge Dictionary is still growing.


1 Answers

Can I make sure that in dictionary in C# there will be only a single of a specific value in it?

Not as such (it wouldn't follow the the normal dictionary contract at that point), but it sounds like you effectively want a bi-directional dictionary. You can do that by composing two dictionaries, one going in each direction. I have an answer on another question with sample code for that.

That will allow you to go directly from "value" to "key" - if you have no need for that, you could always just keep a HashSet<TValue> as well as the normal TDictionary<TKey, TValue> and throw an exception if the caller tries to add a value which already exists.

Again, I would urge you not to do this by deriving from Dictionary<,> - instead just compose a Dictionary<,> and a HashSet<>. (I wouldn't even implement IDictionary<,>, as you have additional constraints which normal dictionaries don't include.)

like image 200
Jon Skeet Avatar answered Oct 08 '22 05:10

Jon Skeet