c# – 添加到字典中的字典

这是我的第一篇文章

我不是一个特别自信的程序员,但我到了那里.

我的问题是我有一个

static Dictionary<string,Dictionary<string,List<string>>> testDictionary = ...

如果Dictionary不包含当前键(字符串),我可以轻松添加键和另一个已填充的字典,如此…

testDictionary.Add(userAgentResult,allowdisallowDictionary);

这工作正常,当我尝试添加内部字典时,如果userAgentResult Key已经存在,我的问题就来了.

我希望这样做……

testDictionary[userAgentResult].Add(allowdisallowDictionary);

但.Add方法需要两个参数,即字符串键和列表值.所以我继续写这段代码……

//this list as the dictionary requires a list
    List<string> testDictionaryList = new List<string>();
    //this method returns a string
    testDictionaryList.Add(regexForm(allowResult,url));
    //this will add the key and value to the inner dictionary,the value,and then     
    //add this value at the userAgentKey
    testDictionary[userAgentResult].Add(allowdisallowKey,testDictionaryList);

这也有效,我的问题是这个字典被添加了很多次,而当内部字典已经包含了试图添加的密钥时,它显然是错误的.所以当

解决方法

在这种情况下,您需要做的是不在内部字典中添加条目.您需要将值添加到外部字典的键值对.只有这次价值恰好是另一个字典:)

testDictionary [userAgentResult] = allowdisallowDictionary;

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...