首页 > 解决方案 > 为嵌套字典添加价值

问题描述

如果我有:

var dict = new Dictionary<Dictionary<string, string>, Dictionary<int, int>>();

如何向嵌套字典添加值。这样做的正确语法是什么?

dict.Add(...)

我知道 > 的语法。但不是两个嵌套字典合二为一。

标签: c#

解决方案


并不是说使用字典作为另一个字典的键很有意义..

var dict1 = new Dictionary<string, string>();

var dict2 = new Dictionary<int, int>();

dict.Add(dict1, dict2);

or 

dict.Add(new Dictionary<string, string>(), new Dictionary<int, int>());

或使用初始化程序的一些数据

dict.Add(new Dictionary<string, string>() { { "test", "test"} }, new Dictionary<int, int>() { {1, 1}});

推荐阅读