Search code examples
.netarraysc++-cli

How to check array<object,N> size, with N a natural number?


My question is simple. How can I know the size of an array declared as...?

array<ObjectType^, 3>

I've seen that the access to their elements is made by the next way:

array_var[i,j,k];

So, how can I know the size of the different dimensions?


Solution

  • It looks like you're using a managed array in c++/cli (though as of time of writing, you haven't got round to confirming this yet).

    On the assumption that you are using a cli::array instance, you can see from its documentation that it inherits from System::Array, and all methods of that class are applicable. If you read that class' own documentation, you'll find that there's an Array::GetLength(Int32) method that lets you get the size of a given dimension (see that method's documentation here).

    For example,

    array<ObjectType^, 3>^ array_var = gcnew array<ObjectType^, 3>(3, 4, 5);
    

    declares a new 3d array of size3*4*5.

    array_var->GetLength(2)
    

    will return 5 (because the dimensions are numbered from 0, as usual). You can also use array_var->Rank to get the number of dimensions for the array.