简体   繁体   中英

What is the appropriate data structure for this array in C#?

If I've got this data structure in PHP, what data structure is the best for it in C#?

$array = Array(
  [dummy1] => Array (
                [0] => "dffas",
                [1] => "asdas2",
                [2] => "asdasda"
              ),
  [dummy2] => Array (
                [0] => "asdasd",
                [1] => "sdfsdfs",
                [2] => "sdfsdf"
              )
)

And so on. But I need to be able to add data to the nested array on the fly, something like this:

$array["dummy1"][] = "bopnf";

I tried using a Hashtable, but when I go hashtable.add(key,value) , where key is my dummy1 , and value is one of the array elements, it will throw an error stating that an element already contains that key.

So I'm thinking a hashtable is not the correct way of tackling this.

For example you could use a Dictionary<string, List<string>> . So you could do:

var dict=new Dictionary<string, List<string>>();
dict.add("dummy1", new List<string>() {"dffas", "asdas2", "asdasda"});
dict.add("dummy2", //etc...);

And to add data:

dict["dummy1"].Add("bopnf");

散列表出错的原因是每个键都必须是唯一的,所以这可能不是最好的方法,最好使用Konamiman解释的字典

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