2014-01-17 44 views
0

我想使对象的第一个元素为string,第二个元素为list。列表对象可以是int/string。我不知道如何实现这个功能。请帮助。以下类型的数据,我需要:c中的多维集合#

Key   Value 
mon_number  {1,4,5} 
mon_name  {jan,apr,may} 
quater_number {1,2} 

回答

2

如果Key是独一无二的,你可以使用Dictionary<string, List<object>>

Dictionary<string, List<object>> dict = new ...; 
dict["mon_number"] = new List<object>(); 
dict["mon_number"].Add(1); 
dict["mon_number"].Add(2); 
dict["mon_number"].Add(5); 
dict["mon_name"] = new List<object>(); 
dict["mon_name"].Add("jan"); 
dict["mon_name"].Add("apr"); 
dict["mon_name"].Add("may");  
... 

你也可以使用数组(如果该元素在列表中的类型是等于所有条目):

Dictionary<string, object[]> dict = new ...; 
dict["mon_number"] = new [] {1, 2, 5 }; 
dict["mon_name"] = new[] { "jan", "apr", "may" } 
+0

我可以直接添加列表:dict [“mon_number”] .add(xyzList); – Dhwani

+0

你的意思是'AddRange'? –

+0

嗯..意思,如果我列出mon_number像列表 mon_numberList =新列表(); mon_numberList.Add(1); mon_numberList.Add(4); mon_numberList.Add(5);并将其添加到字典中,如:dict [“mon_number”] .add(mon_numberList); – Dhwani