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

查看:28
本文介绍了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 中它看起来像这样:

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

在 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天全站免登陆