2014-11-23 109 views
0

我想找到的东西,我可以有一个包含键/值列表中的键/值列表,所以它看起来是这样的:多维关联数组

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#中?

回答

4

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

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

这看起来很有希望! – 2014-11-23 23:10:01

+0

是啊..通用字典非常灵活,会给你比Hashtable类更好的性能! – 2014-11-23 23:18:27