4

In C#, there are three types of arrays: one-dimensional, jagged, and multi-dimensional rectangular.

The question is: given an array of a specific size, how can we create a new array with the same dimensions and rank?

In the case of a multidimensional rectangular array, there appears to be no syntax by which it is possible to define the size and rank (number of dimensions) at runtime.

C# declares multidimensional arrays with commas in the indexers:

object[,,] myArray = new object[2,4,2];

In the above example, I can determine the shape of the array by invoking the Rank property and the size of each dimension by calling the GetLength method and passing the specified dimension.

However, even though I can determine that myArray is 2 x 4 x 2, how can I programmatically create a new instance of an array with the same dimensions, if I am not given the rank of the array beforehand?

Cœur
  • 37,241
  • 25
  • 195
  • 267
nicholas
  • 2,969
  • 20
  • 39

3 Answers3

6

Use Array.CreateInstance(Type, Int32[]) method to create an array of an arbitrary size.

But the problem, you will have after creating this array is: How do you efficiently access the elements of the array if you don't know its rank?

You may use myArray.GetValue(Int32[]) and myArray.SetValue(Object, Int32[]) but I assume the performance is no that good.

To sum up:

public static Array CreateArray(Array array)
{
    // Gets the lengths and lower bounds of the input array
    int[] lowerBounds = new int[array.Rank];
    int[] lengths = new int[array.Rank];
    for (int numDimension = 0; numDimension < array.Rank; numDimension++)
    {
        lowerBounds[numDimension] = array.GetLowerBound(numDimension);
        lengths[numDimension] = array.GetLength(numDimension);
    }

    Type elementType = array.GetType().GetElementType();  // Gets the type of the elements in the input array

    return Array.CreateInstance(elementType, lengths, lowerBounds);    // Returns the new array
}

Update

I've done a little benchmark to compare performance of the array indexer and the GetValue, SetValue versions.

Here is the code I used:

const int size = 10000000;
object[] array1 = new object[size];
object[] array2 = new object[size];

Random random;
random = new Random(0);
for (int i = 0; i < size; i++)
{
    array1[i] = random.Next();
    array2[i] = random.Next();
}

Stopwatch stopwatch = new Stopwatch();

Console.ReadKey();
stopwatch.Restart();
for (int i = 0; i < size; i++)
    array1[i] = array2[i];
stopwatch.Stop();
Console.WriteLine("Indexer method: {0}", stopwatch.Elapsed);

random = new Random(0);
for (int i = 0; i < size; i++)
{
    array1[i] = random.Next();
    array2[i] = random.Next();
}

Console.ReadKey();
stopwatch.Restart();
for (int i = 0; i < size; i++)
    array1.SetValue(array2.GetValue(i), i);
stopwatch.Stop();
Console.WriteLine("Get/SetValue method: {0}", stopwatch.Elapsed);

The result are:

Indexer method: 0.014 s
Set/GetValue method: 1.33 s

The result are slightly different if I replace the int by object.

Indexer method: 0.05 s
Set/GetValue method: 0.54 s

This can be easily explained by the necessary boxing/unboxing when using integer with Set/GetValue.

Cédric Bignon
  • 12,892
  • 3
  • 39
  • 51
  • Exactly what I was looking for. I guess I just didn't search for it using the right terms. I am not so sure about the assumption that `SetValue` and `GetValue` have poor performance, though. I imagine that normal indexing operations are invoking similar methods behind the scenes. – nicholas Jul 23 '13 at 21:01
  • 1
    @Nicholas I've done a little benchmark and the result is quite... obvious (check the post update). – Cédric Bignon Jul 23 '13 at 21:41
  • After reading this i felt the need to check if this compiles: `int[] myLengthsArray = new int[2] { 2, 2 }; int[,] array = (int[,]) Array.CreateInstance( typeof(int), myLengthsArray );` And that does compile, it does defeat the point of the whole thing a bit though – Adam May 10 '17 at 09:31
  • I am confused. I don't see anything wrong with your benchmark but if you look at lines 705-708 from https://referencesource.microsoft.com/#mscorlib/system/array.cs it appears @nicholas is correct? – Michael Wagner Apr 17 '21 at 16:13
3

You can use reflection-like methods:

O. R. Mapper
  • 20,083
  • 9
  • 69
  • 114
2

You can populate an array of lengths (one per rank) based on the existing array, and then use Array.CreateInstance(Type, int[]). So:

public static Array CreateNewArrayOfSameSize(Array input)
{
    int[] lengths = new int[input.Rank];
    for (int i = 0; i < lengths.Length; i++)
    {
        lengths[i] = input.GetLength(i);
    }
    return Array.CreateInstance(array.GetType().GetElementType(), lengths);
}

It's a shame there isn't a method to return all the lengths in one go, but I can't see one. Also I'm surprised that there isn't an ElementType property on Array, but again it may just be eluding me.

Note that this doesn't attempt to maintain the same upper/lower bounds for each dimension as the original array - just the sizes.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194