C#中的多维关联数组

时间:2014-11-23 23:01:53

标签: c# arrays list arraylist

我试图找到一些我可以拥有包含键/值列表的键/值列表的东西,所以它看起来像这样:

String Item 1
    +- key1 -> Object
    +- key2 -> Object
String Item 2
    +- key1 -> Object
    +- key2 -> Object
    +- key3 -> Object
    +- key4 -> Object
String Item 3
    +- key1 -> Object

在php中它看起来像这样:

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
    )
);

C#中有什么可以做的吗?

1 个答案:

答案 0 :(得分:9)

您可以使用使用通用字典作为值的通用字典:

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"];