Mic*_*l B 6 .net c# dictionary
我不是一个特别自信的程序员,但我到了那里.
我的问题是我有一个
static Dictionary<string, Dictionary<string, List<string>>> testDictionary = ...
Run Code Online (Sandbox Code Playgroud)
如果Dictionary不包含当前键(字符串),我可以轻松添加键和另一个已填充的字典,如此...
testDictionary.Add(userAgentResult, allowDisallowDictionary);
Run Code Online (Sandbox Code Playgroud)
这工作正常,当我尝试添加内部字典时,如果userAgentResult Key已经存在,我的问题就来了.
我希望这样做......
testDictionary[userAgentResult].Add(allowDisallowDictionary);
Run Code Online (Sandbox Code Playgroud)
但.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);
Run Code Online (Sandbox Code Playgroud)
这也有效,我的问题是这个字典被添加了很多次,而当内部字典已经包含了试图添加的密钥时,它显然是错误的.所以当
我可能会通过使用一本字典并连接键来简化这一点,从而“模拟”分组。
string key = userAgentResult + allowDisallowKey;
static Dictionary<string, List<string> testDictionary = ...
testDictionary[key] = list;
Run Code Online (Sandbox Code Playgroud)
您只需要管理一本字典即可。
在这种情况下,您需要做的不是向内部字典添加条目。您需要将值添加到外部字典的键值对中。只是这一次该值恰好是另一个字典:)
testDictionary[userAgentResult] = allowDisallowDictionary;
| 归档时间: |
|
| 查看次数: |
37537 次 |
| 最近记录: |