Question

in c# how do i add and then print the values in the following dictionary? Dictionary<String,...

in c# how do i add and then print the values in the following dictionary?

Dictionary<String, HashSet<String>> d

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi,
Here in Dictionary<String, HashSet<String>> d, we have string key and HashSet as the value stored with the key. In C#, we can use the format specified below to add values in a dictionary.

1. Adding values
I am using an example of dictionary variable d, which contains key "Key 1" associated with HashSet values  "Value 1", "Value 2" and key "Key 2" associated with HashSet values  "Value 3", "Value 4".

Dictionary<String, HashSet<String>> d = new Dictionary<String, HashSet<String>>(); // Declaration and initialization of // dictionary variable d

d.Add("Key 1", new HashSet<string> { "Value 1", "Value 2" }); // Here key is "Key 1" and HashSet contains // values "Value 1"and "Value 2"
d.Add("Key 2", new HashSet<string> { "Value 3", "Value 4" });    // Here key is "Key 2" and HashSet contains // values "Value 3" and "Value 4"

2. Printing Values
We can use the following code to access and print each key and value in this dictionary.

foreach(var key_variable in d.Keys) // Loop to get each key in dictionary
{   
Console.WriteLine(key_variable.ToString()); // Printing key on console

foreach (var value in d[""+ key_variable]) // Loop to get each value in HashSet stored with the key
{
Console.WriteLine(value.ToString()); // Printing value in HashSet on console
}
}

You can use Messageboxes also to display the values.
I have pasted the console output of the above example in code.

That's all for now.
Please mention any queries or doubts in the comments section.
Thank You.

Add a comment
Know the answer?
Add Answer to:
in c# how do i add and then print the values in the following dictionary? Dictionary<String,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a c program to make a dictionary .... we add string in file and if we search with only one ...

    write a c program to make a dictionary .... we add string in file and if we search with only one letter then print all its same first letter word and if we enter the complete name then print the only same number.....it can be done only with linked lists...

  • Which of the statements about dictionary Values is false? a. Values of a dictionary must be...

    Which of the statements about dictionary Values is false? a. Values of a dictionary must be unique b. Values of a dictionary can be a mixture of letters and numbers c. More than one Key can have the same value d. The value of the dictionary d for Key k can be accessed as d[k] Consider the following Python statement y = " this is quiz number 4".split() What is the data type of the variable y ? a. list...

  • in Mips how do i get a input string from a user and then print it...

    in Mips how do i get a input string from a user and then print it to the screen? for example if a user enters A6B4GYJx how do i get that and then print it to the screen?

  • I need help defining a class in C++ that acts like a python dictionary. I need...

    I need help defining a class in C++ that acts like a python dictionary. I need to be able to add keys-values to an already existing dictionary. For example id= {'sam':75, 'robert':09907, 'timmmy',95453, 'samuel',5333)}. i want to create a class in which i can do id.add("samuel', 8439922) and it adds it to the dictionary. i want to implement this with using vectors to store the pairs.

  • I need help defining a class in C++ that acts like a python dictionary. I need...

    I need help defining a class in C++ that acts like a python dictionary. I need to be able to add keys-values to an already existing dictionary. For example id= {'sam':75, 'robert':09907, 'timmmy',95453, 'samuel',5333)}. i want to create a class in which i can do id.add("samuel', 8439922) and it adds it to the dictionary. i want to implement this with using vectors to store the pairs. I need to do this without using any features like maps, every thing has...

  • You are given an input C++ string and a dictionary of English words. You are asked...

    You are given an input C++ string and a dictionary of English words. You are asked to see if the input is consisted of words only in the dictionary. For example: Example #1 Input string = "joe"; Input dictionary = { "joe1", "joe" }; Output: true Explanation: "joe" is in the dictionary. Example #2 Input string = "joey"; Input dictionary = { "joe1", "joe" }; Output: false Explanation: even though "joe" is in the dictionary, "y" is not. Example #3...

  • Modify your program from Learning Journal Unit 7 to read dictionary items from a file and write the inverted dictionary to a file. You will need to decide on the following:

    Modify your program from Learning Journal Unit 7 to read dictionary items from a file and write the inverted dictionary to a file. You will need to decide on the following:How to format each dictionary item as a text string in the input file.How to covert each input string into a dictionary item.How to format each item of your inverted dictionary as a text string in the output file.Create an input file with your original three-or-more items and add at...

  • C++ Coding Question on parsing a string If i have a string like "ADD X1, X2,...

    C++ Coding Question on parsing a string If i have a string like "ADD X1, X2, X3" and i wanted to split the string as follows: ADD X1 X2 X3 what is a style of code i can use to parse the string into obtaining each of the following without taking things such as commas and spaces.

  • In C code how do I print characters for card suits. I need to print the...

    In C code how do I print characters for card suits. I need to print the actual symbol for heart, diamond, club, spade.

  • Python Modify your program from Learning Journal Unit 7 to read dictionary items from a file...

    Python Modify your program from Learning Journal Unit 7 to read dictionary items from a file and write the inverted dictionary to a file. You will need to decide on the following: How to format each dictionary item as a text string in the input file. How to covert each input string into a dictionary item. How to format each item of your inverted dictionary as a text string in the output file. Create an input file with your original...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT