Difference between revisions of "Introducing C Sharp Arrays"

From Techotopia
Jump to: navigation, search
(New page: Arrays are certainly not unique to C#, in fact just about every other programming and scripting language preceding the introduction of C# provided support for arrays. Anarray allows a coll...)
(No difference)

Revision as of 18:36, 22 January 2008

Arrays are certainly not unique to C#, in fact just about every other programming and scripting language preceding the introduction of C# provided support for arrays. Anarray allows a collection of values of the same time to be stored and accessed via a single variable. Each item is accessed in the array variable through the the use of an index value into the array.

C# arrays, whilst useful, have some limitations. Perhaps the most significant limitations are the fact that once an array has been created it cannot be made larger or smaller to accommodate more or fewer values. For more dynamic collection storage capabilities, it is worth also reading about C# Collection Classes.

Creating Arrays in C#

A C# array may be created in a number of different ways. One way is to simply declare an array without initializing it with any values. The syntax for this is as follows:

type[] arrayname;

In the above example, type represents the type of data to be stored in the array (or example, string, int, decimal etc). The square brackets ([]) indicate that this is the declaration for an array and arrayname is the name by which the array is to referred.

For example, we can declare an array of strings called myColors as follows:

string[] myColors;

In this example, we have declared the array but not assigned any values to it. To assign values after an array has been declared the new statement must be used combined with a comma separated list of values:

string[] myColors;

myColors = new string[] {"red", "green", "yellow", "orange", "blue"};

An array may also be initialized in the declaration line simply by placing the comma separated list of values after the declaration:

string[] myColors = {"red", "green", "yellow", "orange", "blue"};

Another option is to declare the size of the array when it is created. For example, to declare an array of size 10 simply place the the size value within the square brackets of the new statement:

myColors = new string[5];

This will reserve the space required for the fulkl array without actually placing any values into the array. Finally, this approach may also be combined with the comma separated value list (although the number of values must match the size specified):

string[5] myColors = {"red", "green", "yellow", "orange", "blue"};

Declaring a Multidimensional Array

Multidimensional arrays are declared by placing commas within the square brackets. For example to declare a two dimensional array:

string[,] books;

A two dimensional array is initialized as follows:




Accessing Array Values

Once values have been stored in an array it is highly likely that these values will need to accessed at some later point in the C# code. This is achieved using the array accessor notation combined with the index into the array of the desired value. The array accessor is simply the array name followed by square brackets. Within the square brackets is placed a number representing the index into the array of the desired value (keeping in mind that the first array element is 0). For example, to access the second element of our myColors array the following notation would be used:

myColors[1];
</pre<

For example:

<pre>
string[] myColors = {"red", "green", "yellow", "orange", "blue"};

System.Console.WriteLine(myColors[1]);

When executed, the above code will output the work "green" since this is the string contained at index 1 position in the array.

Similarly, the value at a particular index point in the array may be changed using the accessor notation combined with the assignment operator (=). For example, to change the value of the first item in the array:

myColors[0] = "indigo";