0
Dictionary<string, string> dict = new Dictionary<string,string>(); dict.Add("Hello", "Goodbye"); dict.Add("Morning", "Evening"); dict.Add("Blue", "Red"); foreach(KeyValuePair<string,string> item in dict) { Console.WriteLine("Key = {0}, Value = {1}", dict.Keys, dict.Values); } Console.ReadLine(); 

Looking to get the keys and values as the output, but am getting the following:

Key = System.Collections.Generic.Dictionary2+KeyCollection[System.String,System.String], Value = System.Collections.Generic.Dictionary2+ValueCollection[System.String,System.String] Key = System.Collections.Generic.Dictionary2+KeyCollection[System.String,System.String], Value = System.Collections.Generic.Dictionary2+ValueCollection[System.String,System.String] Key = System.Collections.Generic.Dictionary2+KeyCollection[System.String,System.String], Value = System.Collections.Generic.Dictionary2+ValueCollection[System.String,System.String]

Any advice on getting in the right direction would be great, followed the documentation on https://msdn.microsoft.com/en-us/library/bb346997(v=vs.110).aspx

3 Answers 3

2

Basically you are iterating a collection called dict and during iteration inside foreach loop you are taking each element of the dict collection to item variable. And the problem of your code is dict is a collection so you cannot access it's property as like a single element. Better you change your code like

 foreach(KeyValuePair<string,string> item in dict) { Console.WriteLine("Key = {0}, Value = {1}", item.Key, item.Value); } Console.ReadLine(); 
Sign up to request clarification or add additional context in comments.

2 Comments

You absolute can access the properties of dict inside the foreach, it's just that it's not the correct thing to do in this case.
@Mostafiz Thank you for the explanation I really appreciate it! Much more clear now! Thanks again!
1

dictionary.Keys and dictionary.Values returns collection of keys or values.
Console.WriteLine formats values by calling .ToString().

So you got exactly what your code is doing

dict.Keys.ToString() // System.Collections.Generic.Dictionary2+KeyCollection[System.String,System.String] dict.Values.ToString() // System.Collections.Generic.Dictionary2+ValueCollection[System.String,System.String] 

When you iterating through dictionary, on every iteration you will get instance of type KeyValuePair which contains key and correspondent value. That is why you should use iteration item for accessing required values

foreach(KeyValuePair<string,string> item in dict) { Console.WriteLine("Key = {0}, Value = {1}", item.Key, item.Value); } 

1 Comment

For the OP in case it's not obvious: calling ToString() on a type that doesn't override it returns the default implementation which is the name of the class (in this case it includes a nested class--after the "+"). It doesn't make much sense to have a ToString implementation of a collection type. Recognizing this fact may help to more quickly debug similar situations in the future
0

Change your WriteLine to this:

 Console.WriteLine("Key = {0}, Value = {1}", item.Key, item.Value); 

3 Comments

I will mark it as correct when I can, can you explain why you used item.Key and item.Value as opposed to dict.Keys and dict.Values? Would greatly appreciate it.
Its the way foreach loops work. Every iteration of the loop the new object will be item since that how you declared it. Using dict.Keys and dict.Values is the collection of keys and values not an individual one.
While showing the correct solution, one of the other answers is surely better as they explain what the actual problem is.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.