enumerate keys in in a System.Collections.Generic.Dictionary<string,string>
At debug time I would like to see what are the keys in my InitParams collection - I can't seem to be able to list them.
EDIT:
As Jon suggests below, this might be a bug within the Silverlight debugger. To reproduce, just create a new Silverlight Application within Visual 开发者_运维问答Studio 2010
and just edit code{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
var dictionary = new Dictionary<string, string> {{"A", "1"}, {"B", "2"}, {"C", "3"}};
}
}
}
Assuming you only want the keys, use the Keys
property:
foreach (string key in dict.Keys)
{
...
}
If you want to just get all the keys in an easy-to-read manner in the immediate window, you could use:
string.Join(";", dict.Keys)
or pre-.NET 4:
string.Join(";", dict.Keys.ToArray())
... or if you're using .NET 2, something like this:
string.Join(";", new List<string>(dict.Keys).ToArray())
If you want to get the values at the same time, you can iterate over the KeyValuePair
entries as per Yaakov's answer.
EDIT: I would have expected Visual Studio to show you a nice representation of your dictionary by default, to be honest. For example, this is what I see in VS2008:
... and I've just tried it in VS2010 and seen the same result. Under the Debugging general options, have you got "Show raw structure of objects in variable windows" ticked? If so, untick it.
Programatically:
foreach (KeyValuePair<string,string> param in InitParams) {
Debug.Writeline(param.Key + ": " + param.Value);
}
In the debugger, drilldown into InitParams > Values > Non-Public members > dictionary. It should show up eventually.
In the Immediate window, try something like InitParams["abc"]
to show the value of the first item, where "abc" is a known Key in the collection. If you don't know the names of the Keys in the collection, then use the programmatic method above to write all of the values to the Debug window.
精彩评论