I have the following code snippet to add string values to a List and then add the list as the key in a dictionary. Now I want to print both the key and the value from the Dictionary but I'm unable to do so. Any ideas or suggestions will be appreciated.
Dictionary, int> dic = new Dictionary, int>();
List mylist = new List();
string[] str = new string[5];
int counter = 0;
for (int i = 0; i < 5; i++)
{
Console.Write("Type Number:");
string test = Console.ReadLine();
mylist.Add(test);
counter++;
}
Console.WriteLine(string.Join(",", mylist));
dic.Add(mylist, counter);
Talk1:
Side note: are you sure List should be the key of dictionary, not its value?
Talk2:
Well, it's up to you. But it looks very curious.... At least you have to create custom comparator in the case of List to be the key. Otherwise it is possible to add duplicates into your dictionary.
Talk3:
Poor design. As @AndyKorneyev pointed out, List should not be the key to a dictionary. How would you even begin to look up an item? I can't think of one possible reason to do that.
Talk4:
- I agree about using objects as keys but a List has no override for GetHashCode so uses the default inherited from object. So its fine if you just want to iterate over the dictionary but you can't reliably use it as a lookup because a new List instance with the same string values will produce a different HashCode so different key. So why not just reverse the key/value pair as the key is useless in this case?
Talk5:
yes - all valid points, and based on comments and responses from OP I've now seen on some of the answers, you are probably right. I simply try not to make assumptions about how the OP intends to use the code in question. In this case, I might turn your question around and ask "what use is storing the int as a key" - in other words, why not just use an array which would achieve the same purpose? Two possible conclusions: either OP is a beginner programmer that's just learning, or there was intention behind his question. Probably A, but not always good to assume that. :)
Solutions1
Try this
foreach(var v in dic)
Console.WriteLine(string.Join("," v.Key) + " " + v.Value.ToString());
Although, it does seem like you have your key and value backwards...
Solutions2
If you want to have string as you key (as you indicate by joining the List, then consider making a Dictionary instead of Dictionary,int>:
Dictionary dic = new Dictionary(); //note this dict type
List mylist = new List();
string[] str = new string[5];
int counter = 0;
for (int i = 0; i < 5; i++)
{
Console.Write("Type Number:");
string test = Console.ReadLine();
mylist.Add(test);
counter++;
}
string key = string.Join("", mylist); //note this key combination
//you will have key-value pair such as "12345"-5
Console.WriteLine(string.Join(",", mylist)); //note, you will print as "1,2,3,4,5"
dic.Add(key, counter);
And print it out like what has been shown:
foreach(var v in dic)
Console.WriteLine(v.Key.ToString() + " " + v.Value.ToString());
Original:
using foreach on each Dictionary element will do the job:
foreach(var v in dic)
Console.WriteLine(v.Key.ToString() + " " + v.Value.ToString());
The foreach will allow you to iterate over every element in your Dictionary.
Additionally, you may consider to reverse your Dictionary key and value:
Dictionary> dic = new Dictionary>();
For it is quite uncommon to call int from List. The Key part of the Dictionary is normally the simpler one. Also, by having a List as Key, you have to have that exact List to call the Value, but having int as you Key will allow you to get List from int pretty easily.
And if you plan to join the List you should use or rather than ,int> or >
Talk1:
not working, as in? Seems like the Dictionary is reversed
Talk2:
Actually OP didn't mention what was happening and what he was expecting. I just thought his string.join wasn't printing. Forget it
Talk3:
It is printing the list values as System.Collections.Generic.List`1[System.String]
Talk4:
so you actually want your List to be joined as Key are you?