Arrays in .NET – part I

Currently, the CLR supports single-dimension arrays, multi-dimension arrays and jagged arrays (aka, arrays of arrays). Arrays are always implicitly derived from the Array reference type.Creating a single-dimension array is simple, as you can see from the next snippet:

var ints = new Int32[10];

ints references an array which can hold 10  Int32 elements (ie, 10 integers). Since I was initializing the variable in the declaration, then I used the var keyword to reduce the typing. If I only wanted to declare a variable, then I could have simply written the following:

Int32[] ints;
ints = new Int32[10];

In this case, ints is a variable capable of referencing an array of integers. Notice that ints is null until the execution of the second line. It’s also important to keep in minf that ints refers to a memory space which is allocated in the managed heap (and that means that an array are garbage collected) because all arrays implicitly extend the Array base type.

After making sure that ints references an array, you write and read elements from the array by specifying a position (which is know as index). The next example shows how to do that:

//write value 2 to the first position
ints[0] = 2;
//print 1st value of the array

CLS compliant arrays are always zero based. In other words, the first element of an array is always placed on position 0. Requiring that all arrays to be zero based allows easy sharing of arrays between CLS languages. Since these single-dimension arrays are the most common type of array used in applications, forcing them to be zero-based has allowed MS to optimize its runtime for its use. Notice that I said CLS, not CLR. In fact, the CLR allows you to create non zero based arrays, but you should be prepared for a small performance hit (I’ll return to this topic in a future post).

As I’ve said at the start, you can also have multi-dimension arrays. Here’s a quick example:

//two dimension arrays of 10×10
Int32[,] ints = new Int32[10, 10];
//put 10 in 1st position
ints[0, 0] = 10;

As you can see, it’s simple: you specify the number of elements in each dimension. Notice also the syntax used for the declaration of a multi-dimension array: the comma “specifies” the number of dimensions (if it were a three dimension array, then you’d use two commas like this Int32[,,]). We still need to look at the declaration and use of jagged arrays. In practice, a jagged array is an array of arrays:

Int32[][] ints = new Int32[2][];
ints[0] = new Int32[2];//2 elems array
ints[1] = new Int32[4];//4 elems array
//put something in one of the jagged arrays
ints[0][0] = 1;

As you can see, the [] [] syntax makes it clear that this is an array of arrays. Notice also that, unlike the multi-dimension array, each “dimension” can have a different number of elements (in the previous example, there are 2 elements in the “first dimension” and 4 on the second).

One final note before ending: the CLR ensures that you will only access a valid position of an array. For instance, if you created an array with 20 elements, you’ll end up with an exception when you try to access an element outside of the [0,19] interval. This is a good thing because it ensures that you will always read an element from the array. There is, however, a small cost associated with this strategy. If you think this is really too much, then you can disable it by accessing the array elements through unsafe access (more details on a future post).

And that’s it for now. In the next posts we’ll keep looking at arrays. Stay tuned for more.


~ by Luis Abreu on May 18, 2011.

Leave a Reply

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

You are commenting using your 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: