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 initial开发者_高级运维ized in the same statement? I know by looking that it is [1,3,3], but how do I determine that programmatically?
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.
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.
精彩评论