2014-11-23 204 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