jmq
jmq

Reputation: 10370

How to determine the size of each dimension of multi-dimensional array?

int[,,] moreInts = { {{10,20,30}, {60,70,80}, {111,222,333}} };

How do I retrieve the size of each dimension in this multi-dimensional array when it is defined and initialized in the same statement? I know by looking that it is [1,3,3], but how do I determine that programmatically?

Upvotes: 0

Views: 175

Answers (2)

Ginka
Ginka

Reputation: 570

try with this code snippet:

int[, ,] moreInts = { { { 10, 20, 30 }, { 60, 70, 80 }, { 111, 222, 333 } } };

List<int> indicies = new List<int>();

for (int i = 0; i < moreInts.Rank; i++)
    indicies.Add(moreInts.GetUpperBound(i) + 1);

indicies.ForEach(i => Console.WriteLine(i));

Hope this helps.

Upvotes: 0

David
David

Reputation: 58

The GetLength method can be used to return the size of a array's dimension

moreInts.GetLength(0);
moreInts.GetLength(1);
moreInts.GetLength(2);

This is the correct way for an array declared [,,]. For arrays declared [][][], where each could have a different number of dimensions, KeithS's method is correct.

Upvotes: 3

Related Questions