2

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"]);

3 Answers 3

2

What you are doing is a single-dimensional array of dictionaries:

Dictionary<string,Decimal>[] array =
{
    new Dictionary<string,Decimal>(){ 
        {"height", 10},
        {"width", 20},
        {"length", 30},
    },
    new Dictionary<string,Decimal>(){ 
        {"height", 10},
        {"width", 20},
        {"length", 30},
    },
    new Dictionary<string,Decimal>(){ 
        {"height", 10},
        {"width", 20},
        {"length", 30},
    },
};

Console.WriteLine(array[0]["height"]);

Output: 10

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{}.

Sign up to request clarification or add additional context in comments.

1 Comment

Any time. You did the right thing by figuring out a way to make it work on your own, as it helps you understand better.
2

Perhaps like this:

int[, ,] arrayName = new int[10, 20, 30];
int ten = arrayName.GetLength(0);
int twenty = arrayName.GetLength(1);
int thirty = arrayName.GetLength(2);

Individual values are then accessed like so:

arrayName[2,13,25] = 42;

1 Comment

Thank you! your suggestion works too. Check the edit I made on the question
0

you can simply create your own class array container

class MyArray
{
public int height
{
    get;
    set;
}

public int width
{
    get;
    set;
}

public int length
{
    get;
    set;
}
}

var array = new MyArray[1];
array[0] = new MyArray() { height= 12, width = 32, length = 3 };

var list = new List<MyArray>();
list.Add(new MyArray());

var foo = new MyArray();
foo.height= 55;

var Y1= new MyArray() { height = 23 };

var X1 = new MyArray() { height  = 7 };
var X2 = new MyArray() { height  = 8 };
var list = new List<MyArray>() { X1, X2 };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.