Advertisment

Arrays in C#

author-image
PCQ Bureau
New Update

You will remember from last month that arrays are reference types in .Net's CTS (Common Type System), and hold identical types of data, that is, all integers, or all longs. We also discussed single-dimensional arrays. Now we move on to the juicy part of arrays–multidimensional arrays.

Advertisment

C# supports two kinds of multidimensional arrays, RMA (Rectangular multidimensional arrays), where all rows are of the same size; and JMA (Jagged multidimensional arrays) which are arrays of arrays, resulting in arrays where the rows can be of different sizes. You can have a 2D array where the first row has just three elements, while the second row may have 200 elements, with just the right amount of memory allocated.

Conceptually, the RMA is similar to its traditional counterparts, but differs in the way it's declared.

int <,> rma;
Advertisment

The new declaration syntax takes a comma within the box brackets to specify the number of dimensions. Thus, the above declaration defines a two-dimensional array of integers, while the following declaration declares a three-dimensional array of longs.

int <,,>rma3d;

Now let's see how RMA is brought to life by defining it and allocating memory. The method is the same, using the new operator, as shown below.

Advertisment
int <,> rma;



rma = int <5,3>;

This defines RMA to be a two-dimensional array of the size 5-by-3. Similarly, you can define a three-dimensional array.

int <,,> rma3d;



rma3d = new int <2,2,2>;
Advertisment

Here, rma3d is defined as a three-dimensional array of 2-by-2-by-2 size.

Let’s move on to jagged arrays, C#’s contribution to the developer community. A jagged array is a single-dimensional array of arrays, where you not only specify the dimensions of the jagged array, but also the contained arrays. For example,

int <><> jag;
Advertisment

Here, we have declared jag to be an array. Next, we specify the number of dimensions the jagged array is supposed to have.

jag = new int <4><>;

This statement indicates that jag is a single dimensional array, having four elements, or arrays since it’s a jagged array. Now, each contained array, indexed from 0 to 3, can have independent number of rows, which are specified next.

Advertisment
jag<0> = new int <4>;



jag<1> = new int <2>;


jag<2> = new int <10>;


jag<3> = new int <6>;

To recap, jag is a jagged array that is single dimensional in nature and contains 4 arrays, each of which have different number of elements, which in the above example are 4,2,10, and 6 respectively. So, if I were to assign the value of three to the second element of the third array referenced by the jag variable, the statement to do so would be:

jag<2><1>=3;
Advertisment

That’s all there is to arrays. I have written a small program below to put to use all that we've discussed. The program simply creates a single-dimensional array, a rectangular two-dimensional array, and finally, a jagged array, and displays their dimension, technically called rank, and their length, which is the total number of elements in all the dimensions of the array. The listing is available on the CD accompanying this issue. The program output is described below.

C:\kgk\Articles\PCQ>part3



Array SD has the dimension: 1 and length: 5


Array TD has the dimension: 2 and length: 20


Array JG has the dimension: 1 and length: 2


C:\kgk\Articles\PCQ>


I've used the length and rank properties of the array object to determine the number of elements in the array and the array rank respectively. If you recall from the discussion of CTS, everything is finally an object. The arrays we create are no different–they not only derive from the System.Object class, but also the System.Array class, which is the parent to the arrays we create and child of the System.Object class. Rank and length are the properties made available by the System.Array class to array objects.

If you notice the output and compare it with the definition of the corresponding arrays in the program listing, the values will appear to be consistent with what we've discussed. Pay particular attention to the output concerning the jagged array, which reinforces the fact that jagged arrays are essentially single-dimensional in nature.

Kumar Gaurav Khanna runs www.wintools.f2s.com

Advertisment