O'Reilly logo

C# Primer: A Practical Approach by Stanley B. Lippman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required