I have a generic multidimensional array having different dimensions. I need to loop through it in order to save for each element the relative array of indices and the relative value.
In the case of an array of two dimensions, let's say 4 and 5, it would be trivial:
var d1 = 4;
var d2 = 5;
var array = Array.CreateInstance(typeof (int), new[] {d1, d2});
//...
// code for populating array
//...
for (int i = 0; i < d1; i++)
{
for (int j = 0; j < d2; j++)
{
var value = array.GetValue(new[] { i, j });
var indices = new[] {i, j};
}
}
But I need to do the same thing with an array of n different dimensions. How can I achieve it? Do I need to implement a recursive function?

ndimensional array and just need to write the code for iterating over it?ndimensional array, you can just do a flat iteration through the array and use modulo operators to figure out what index you're at.