5.2. The Array Is a System.Array

All C# array types have access to the System.Array class public methods and properties. For example, the Length property returns the number of elements in an array:

for ( int ix = 0; ix < fib.Length; ++ix )

If the array is multidimensional, Length is a less useful property. For example, for the following array declaration:

static float [,] mat = new float[4,5]
{
      { 1f, 0f, 0f, 0f, 0f },
      { 0f, 1f, 0f, 0f, 0f },
      { 0f, 0f, 1f, 0f, 0f },
      { 0f, 0f, 0f, 1f, 0f }
};

the result of Length is 20. To retrieve the length of the individual dimensions of a multidimensional array, we use GetLength(int dim), where dim represents the number of the dimension: 0, 1, 2, and so on. To discover the number of dimensions associated ...

Get C# Primer: A Practical Approach now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.