Reputation: 6113
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.
Upvotes: 0
Views: 202
Reputation: 3138
the reason your getting the error with the hashtable is because each key needs to be unique so this is probably not the best approach, the use of a dictionary as Konamiman explain would be best
Upvotes: 0
Reputation: 50293
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");
Upvotes: 2