简体   繁体   中英

Multi-Dimensional collection in c#

I want to make collection like first element in string and second is list of object. list object can be either int / string . I don't know how to achieve this functionality. Pls help. Following kind of data I need:

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

If Key is unique, you could use 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");    
...

You could also use arrays (if the type of the elements in the list is equals for all entries):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM