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

Advertisements

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: