array<System::String^, 2> declares a two dimensional array. Arrays created in this way have a fixed height & width.
array<String^, 2>^ messages =
{
{"a", "b"},
{"c", "d", "e"},
{"f"},
};
Debug::WriteLine("The total number of array elements is {0}", messages->Length);
Debug::WriteLine("Dimension 0 has a length of {0}", messages->GetLength(0));
Debug::WriteLine("Dimension 1 has a length of {0}", messages->GetLength(1));
Debug::WriteLine("{");
for (int i = 0; i < messages->GetLength(0); i++)
{
Debug::Write(" { ");
for (int j = 0; j < messages->GetLength(1); j++)
{
Debug::Write("\"" + messages[i,j] + "\", ");
}
Debug::WriteLine("}");
}
Debug::WriteLine("}");
Output:
The total number of array elements is 9
Dimension 0 has a length of 3
Dimension 1 has a length of 3
{
{ "a", "b", "", }
{ "c", "d", "e", }
{ "f", "", "", }
}
However, based on how you're initializing the array, and how you're looping, it looks like you're interested in a jagged array, not a regular two dimensional (square/rectangular) array. This should be declared as an array of arrays: Each inner array is initialized separately, and therefore each has their own length.
array<array<String^>^>^ messages =
{
{"a", "b"},
{"c", "d", "e"},
{"f"},
};
// The only thing we can tell easily is the number of rows. The total
// number of elements and the column count would require iteration.
Debug::WriteLine("There are {0} rows in the jagged array", messages->Length);
Debug::WriteLine("{");
for (int i = 0; i < messages->Length; i++)
{
Debug::Write(" { ");
for (int j = 0; j < messages[i]->Length; j++)
{
Debug::Write("\"" + messages[i][j] + "\", ");
}
Debug::WriteLine("}");
}
Debug::WriteLine("}");
Output:
There are 3 rows in the jagged array
{
{ "a", "b", }
{ "c", "d", "e", }
{ "f", }
}