简体   繁体   English

枚举System.Collections.Generic.Dictionary中的键<string,string>

[英]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. 在调试时我想看看我的InitParams集合中的键是什么 - 我似乎无法列出它们。

InitParam

EDIT: 编辑:

As Jon suggests below, this might be a bug within the Silverlight debugger. 正如Jon在下面建议的那样,这可能是Silverlight调试器中的一个错误。 To reproduce, just create a new Silverlight Application within Visual Studio 2010 要重现,只需在Visual Studio 2010中创建一个新的Silverlight应用程序 bug silverlight 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: 假设您只需要密钥,请使用Keys属性:

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: 或者在.NET 4之前:

string.Join(";", dict.Keys.ToArray())

... or if you're using .NET 2, something like this: ...或者,如果您使用的是.NET 2,请执行以下操作:

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. 如果要同时获取值,可以按照Yaakov的答案迭代KeyValuePair条目。

EDIT: I would have expected Visual Studio to show you a nice representation of your dictionary by default, to be honest. 编辑:说实话,我希望 Visual Studio默认为您显示一个很好的字典表示。 For example, this is what I see in VS2008: 例如,这就是我在VS2008中看到的:

Visual Studio中的字典

... and I've just tried it in VS2010 and seen the same result. ......我刚刚在VS2010中尝试过,看到了相同的结果。 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. 在调试器中,深入了解InitParams>值>非公共成员>字典。 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. 在立即窗口中,尝试类似InitParams["abc"]来显示第一个项目的值,其中“abc”是集合中的已知密钥。 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. 如果您不知道集合中Keys的名称,则使用上面的编程方法将所有值写入Debug窗口。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将类型字符串隐式转换为System.Collections.Generic.Dictionary <string,System.Collections.Generic.Dictionary><string,object> &gt; - Cannot implicitly convert type string to System.Collections.Generic.Dictionary<string,System.Collections.Generic.Dictionary><string,object>> &#39;System.Collections.Generic.Dictionary的最佳重载方法匹配 <int,System.Collections.Generic.Dictionary<string,int> &gt; .Dictionary(int)&#39; - The best overloaded method match for 'System.Collections.Generic.Dictionary<int,System.Collections.Generic.Dictionary<string,int>>.Dictionary(int)' 无法将System.String强制转换为System.Collections.Generic.Dictionary类型 <string, string> 与会话变量 - Unable to cast System.String to type System.Collections.Generic.Dictionary<string, string> with Session variable 无法从“ System.Threading.Tasks.Task”转换为“ System.Collections.Generic.Dictionary” <string,string> &#39; - Cannot convert from 'System.Threading.Tasks.Task' to 'System.Collections.Generic.Dictionary<string,string>' Firebase 数据库错误:System.Collections.Generic.Dictionary`2[System.String,System.Object] - Firebase Database Error: System.Collections.Generic.Dictionary`2[System.String,System.Object] 无法从 &#39;void&#39; 转换为 &#39;System.Collections.Generic.Dictionary<string, bool> &#39; - Cannot convert from 'void' to 'System.Collections.Generic.Dictionary<string, bool>' fb 事件代码统一参数 2:无法从 'System.Collections.Generic.Dictionary 转换<string, object> ' 浮?'</string,> - fb event code unity Argument 2: cannot convert from 'System.Collections.Generic.Dictionary<string, object>' to 'float?' System.Collections.Generic.Dictionary =终极表现? - System.Collections.Generic.Dictionary = Ultimate performance? System.Collections.Generic.Dictionary foreach顺序 - System.Collections.Generic.Dictionary foreach order 将System.Collections.Generic.Dictionary转换为XDocument - Turn a System.Collections.Generic.Dictionary into an XDocument
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM