Wednesday, March 10, 2010

Arrays (C#, .NET, Visual Studio 2008)


In previous DevCasts, we accessed command-line parameters by way of the parameter array. We saw that arrays are declared using square brackets ("[]"), that every array-type has a "length" parameter, and that we could access elements of an array using a numerical "index". This DevCast will give details about different types of arrays and how they can be used in C#.

The solution for this DevCast consists of a single project and code-file--all named "arrays". From now on, I will not describe every aspect of the code but hope you will be able to follow from what you see. If you don't understand the line "using System;", class declarations, or the importance of the "Main" method, please watch one of the previous DevCasts.

The first type of array we will look at is called a "single-dimensional array". The parameter array created through command-line arguments is of this type. To declare a single-dimensional array for our own use, we first must declare the type of each element, then give it a name (in this case, "numbers"), and then assign it an instance of a new array object of the same type with a predefined length.

You can think of a single-dimensional array as a single list of objects. We set the "numbers" array to be length 5 and we can test this by getting the "Length" property and writing it to the console. The length of 'numbers' is 5 just as we declared it and expected it to be.

The next type of array to look at is what's called a "multidimensional array". We're going to make a 2-dimensional array because that is the simplest to visualize and to work with but there's no limit to the number of dimensions. To create the 2-dimensional array we start off with the element type, as always, but in the square brackets, we insert a comma to indicate that one index will be on the left, and another index will be on the right. This makes more sense when assigning the new array object. First, we'll give it the name "names" and assign it a new string array with the size of one dimension 5 and the size of the other dimension 4.

You can visualize this as a table containing 5 rows and 4 columns. Or maybe, you would prefer 5 columns and 4 rows. Let's find out how the computer thinks about this by printing the "Length" of this array to the console.

The computer says the length of the "names" array is 20 elements long. Basically, this means the computer is placing each dimension end-to-end to create a long list and calculating the position of any one element given the indices we provide. It's best that we continue to think of it as a table because of this.

The final type of array is called a "jagged array" or, more technically, an "array-of-arrays". The multidimensional array we saw before is such that every row of elements has the same number of columns. In the jagged array, each row can be of a different length. This is because the jagged array is created by declaring an array where each element is also an array.

Declaring the jagged array may look different from the single-dimensional array, but, in fact, they are extremely similar. To illustrate the similarity, let's start off by making a single-dimensional array of bytes. We'll make it 5 bytes long just like the "numbers" array we made earlier and we'll give it the name "scores". We can think of this byte array as a single data-type just like "int" or "string" so, if we want to make an array of byte arrays, we just add the square brackets to the end of this type. Notice the empty brackets at the end of this line. We do that because we don't know the size of each internal array.

To prove that "scores" is a simple single-dimensional array, let's print the length to the console. This tells us that scores contains 5 elements. The only difference between "scores" and "numbers" is each element of "scores" is an array-type whereas each element of "numbers" is an integer-type.

The internal arrays of "scores" do need sizes before we can work with them, so let's do it programmatically with a for-loop. We want to populate every element of "scores" so we start the loop at "0" and continue until "scores.Length". The definition of "scores" says that each element must be a byte-array so we say "scores[i] = new byte[i+3];" Notice, this will force each internal array to be a different size.

Remember that we access an element of an array by putting the position of that element in square brackets after the array's name. Since each element of the "scores" array is also an array, we can discover its length with the "Length" property. Looping over the scores array will give us access to each internal array and we can print its length with "scores[i].Length".

Run this one more time in the debugger to see that we can access every array we have created.

I'm Justin Mancinelli; thanks for watching.



Links:
http://msdn.microsoft.com/en-au/library/hh0eh6yz.aspx
http://zpjdevcast.blogspot.com/