multidimensional associative array in C#

こ雲淡風輕ζ 提交于 2019-12-24 17:43:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!