I have been trying to figure this out, but couldn’t.
On php a multidimensional array would looks like this:
$array = array(
1 => array("height" => 10, "width" => 20, "length" => 30),
2 => array("height" => 10, "width" => 20, "length" => 30),
3 => array("height" => 10, "width" => 20, "length" => 30),
);
And them I would get the value using
echo $array[3]["height"]; // output 10
echo $array[3]["width"]; // output 20
I have tried to use Dictionary, but I have no idea on how to accomplish this.
Any help is appreciated
Thanks
EDIT:
This is the code I used that worked for me, hope this helps someone.
Dictionary<int, Dictionary<string, int>> array = new Dictionary<int, Dictionary<string, int>>();
Dictionary<string, int> sub = new Dictionary<string, int>();
sub.Add("height", 10);
sub.Add("width", 20);
sub.Add("length", 30);
array.Add(1, sub);
sub = new Dictionary<string, int>();
sub.Add("height", 10);
sub.Add("width", 20);
sub.Add("length", 30);
array.Add(2, sub);
sub = new Dictionary<string, int>();
sub.Add("height", 10);
sub.Add("width", 20);
sub.Add("length", 30);
array.Add(3, sub);
Response.Write("height: " + array[1]["height"]);
Response.Write("<br />width: " + array[1]["width"]);
Response.Write("<br />length: " + array[1]["length"]);
What you are doing is a single-dimensional array of dictionaries:
In C# you can declare IEnumrables with an Add method (of which both System.Array and Dictionary are children) explicitly with curly braces, so what we are doing here is declaring an array{} and three dictionaries{} inside it, and inside each dictionary three key/value pairs{}.