问题
I am trying to find something where I can have a key/value list that contains a key/value list, so it would look something like this:
String Item 1
+- key1 -> Object
+- key2 -> Object
String Item 2
+- key1 -> Object
+- key2 -> Object
+- key3 -> Object
+- key4 -> Object
String Item 3
+- key1 -> Object
In php It would look like this:
array(
"String Item 1" => array(
"key1" => Object,
"key2" => Object
),
"String Item 2" => array(
"key1" => Object,
"key2" => Object,
"key3" => Object,
"key4" => Object
),
"String Item 3" => array(
"key1" => Object
)
);
Is there anything that can do that in C#?
回答1:
You can use a generic dictionary that uses a generic dictionary as the value:
var dict = new Dictionary<string, Dictionary<string, object>>();
// Adding a value
dict.Add("key", new Dictionary<string, object>()
{
{ "key1", "value1" },
{ "key2", "value2" }
});
// Retrieving value1
var result = dict["key"]["key1"];
来源:https://stackoverflow.com/questions/27095470/multidimensional-associative-array-in-c-sharp