使用字符串索引在c#中定义二维数组

时间:2014-12-24 11:04:41

标签: c# arrays list

如何在c#中定义类似的内容:

list["alpha","beta"] = value;

我想访问诸如字典之类的值:

var item = list["alpha","beta"];

2 个答案:

答案 0 :(得分:2)

你需要的不是二维数组,而是字典字典。 请参阅词典here的文档。

然后你会写myDic [“outerDicKey”] [“innerDicKey”]来检索一个值。

答案 1 :(得分:1)

实现聚合词典的类型:

class Test
{
    private struct Key
    {
        public string Key1 { get; set; }

        public string Key2 { get; set; }

        public Key(string key1, string key2) 
            : this()
        {
            Key1 = key1;
            Key2 = key2;
        }
    }

    private readonly Dictionary<Key, object> _dictionary = new Dictionary<Key, object>();

    public object this[string key1, string key2]
    {
        get { return _dictionary[new Key(key1, key2)]; }
        set { _dictionary[new Key(key1, key2)] = value; }
    }
}