如何在c#中使用这个定义与字典

时间:2011-11-16 12:51:44

标签: c# dictionary

GROUP["10"]["MATH"] = 30;
GROUP["11"]["MATH"] = 40;
GROUP["9"]["CHEM"] = 50;
...

你能告诉我如何在字典中使用它吗?如何为这个例子定义字典?

8 个答案:

答案 0 :(得分:5)

Dictionary<string,Dictionary<string,int>>

第一个[]返回一个字典,第二个[]操作该字典并返回一个int

答案 1 :(得分:4)

Dictionary<string, Dictionary<string, int>>

使用:

Dictionary<string, Dictionary<string, int>> dic = new Dictionary<string, Dictionary<string, int>>();
Dictionary<string, int> inner = new Dictionary<string, int>();
inner.Add("MATH", 30);
dic.Add("10", inner);

...

访问

dic["10"]["MATH"]

答案 2 :(得分:4)

使用这个......

 Dictionary<string,Dictionary<string,int>>  

请通过此链接获取更多信息....在字典中,如果您不熟悉字典..

http://msdn.microsoft.com/en-us/library/xfhwa508.aspx

答案 3 :(得分:4)

你可以创建一个Dictionary<Tuple<string, string>, int>,但你最好定义一个类/自定义集合来更好地表示它,这取决于你的用例。

Dictionary<Tuple<string, string>, int>

的示例
var dict = new Dictionary<Tuple<string, string>, int>
           {
               { new Tuple<string,string>("10", "MATH"), 30 },
               { new Tuple<string,string>("11", "MATH"), 40 },
               { new Tuple<string,string>("9", "CHEM"), 50 }
           };

答案 4 :(得分:2)

选项1 :使用Dictionary<string, Dictionary<string, int>>

Dictionary<string, int> group10 = new Dictionary<string, int>();
group10.Add("MATH", 30);
Dictionary<string, int> group11 = new Dictionary<string, int>();
group10.Add("MATH", 40);
Dictionary<string, int> group9 = new Dictionary<string, int>();
group10.Add("CHEM", 50);
var group = new Dictionary<string, Dictionary<string, int>>();
group.Add("10", group10);
group.Add("11", group11);
group.Add("9", group9);

int v1 = group["10"]["MATH"]; // v1 = 30
int v2 = group["11"]["MATH"]; // v2 = 40
int v3 = group["9"]["CHEM"]; // v3 = 50

选项2 :或者为覆盖GetHashCodeEquals的字典创建复合键,或者使用Tuple

var group = new Dictionary<Tuple<string, string>, int>();
group.Add(Tuple.Create("10", "MATH"), 30);
group.Add(Tuple.Create("11", "MATH"), 40);
group.Add(Tuple.Create("9", "CHEM"), 50);

int v1 = group[Tuple.Create("10", "MATH")]; // v1 = 30
int v2 = group[Tuple.Create("11", "MATH")]; // v2 = 40
int v3 = group[Tuple.Create("9", "CHEM")]; // v3 = 50

如果使用选项1,您必须记住在添加整数值之前创建第二级Dictionary<string, int>

答案 5 :(得分:1)

这样的事情可能是

Dictionary<string, Dictionary<string><int>> dict = new Dictionary<string, Dictionary<string><int>>(); 

答案 6 :(得分:1)

你也可以创建自己的字典:

public class GroupDict
{
    const string Separator = ";";

    Dictionary<string, int> _internalDict = new Dictionary<string, int>();

    public void Add(string key1, string key2, int value)
    {
        _internalDict.Add(key1 + Separator + key2, value);
    }

    public int this[string key1, string key2]
    {
        get { return _internalDict[key1 + Separator + key2]; }
        set { _internalDict[key1 + Separator + key2] = value; }
    }

    public int Count
    {
        get { return _internalDict.Count; }
    }
}

然后您可以像:

一样使用它
GroupDict groups = new GroupDict();
groups.Add("10", "MATH", 30);
// OR
groups["10", "MATH"] = 30;

Console.WriteLine(groups["10", "MATH"]);

答案 7 :(得分:0)

另请注意,您无法在C#中创建多键字典。

你需要自己的结构。