jbatista
jbatista

Reputation: 2855

Multidimensional arrays in C# parametrized on dimensions

Is it possible (and, so how?) to parametrize a multidimensional array when the number and size of the different dimensions is unknown a priori (i.e. passed as parameters)?

If we define

int[] a1dim;
int[,] a2dim;
int[,,] a3dim;

we already know that a1dim, a2dim and a3dim are 1-, 2- and 3-dimensional respectively. But is it possible to specify the dimensionality of an array when a parameter is passed (e.g. a list of the sizes of each dimension)? e.g.,

public object GetMultidim(int[] sizes)
{
    object[/*?*/] andim = new object[/*func(sizes)?*/];
    return andim;
}

Upvotes: 1

Views: 101

Answers (2)

Preston Guillot
Preston Guillot

Reputation: 6724

Use the Array.CreateInstance factory methods to instantiate your arrays instead of the [dim, dim ...] short hand.

Upvotes: 1

Dark Falcon
Dark Falcon

Reputation: 44181

It already exists: System.Array.CreateInstance(Type, params int[])

Upvotes: 4

Related Questions