在C#中的多维关联数组 [英] multidimensional associative array in C#

查看:221
本文介绍了在C#中的多维关联数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到的东西在那里我可以有一个包含键/值列表中的键/值列表,因此它看起来是这样的:

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

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

Is there anything that can do that in C#?

推荐答案

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

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

这篇关于在C#中的多维关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆