#131 – Arrays Derive from System.Array

In C#, all arrays derive from the abstract class System.Array.  You can directly create objects of type System.Array, but must create arrays using the syntax for declaring and instantiating arrays built into the C# language.

Because arrays inherit from System.Array, all arrays inherit certain methods and properties from the Array class.

For example, every array has the properties:

  • Length – Total number of elements, across all dimensions of the array
  • Rank – Number of dimensions in the array
 int[] nums = new int[4];
 int[,] nums2D = new int[2, 3];
 int[][] numsJagged = new int[2][];
 numsJagged[0] = new int[2];
 numsJagged[1] = new int[3];

 int n = nums.Length;    // 4
 n = nums.Rank;          // 1

 n = nums2D.Length;      // 6
 n = nums2D.Rank;        // 2

 n = numsJagged.Length;  // 2
 n = numsJagged.Rank;    // 1

Note that for the jagged array, because the array  is really an array of arrays, Length and Rank operate on the outer array, which is just a 2-element one-dimensional array.