Click here to download the sample source code for this Arrays lab exercise If you forgot how to open the files using Visual Web Developer, click here to learn How to open C# ASP.NET Website files. Please use these files only as a reference. You should go through the exercises yourself and create your own project according to the instructions in the exercise.
Now that we have seen how conditionals work and practiced looping, it’s time to move on to . Arrays are the most basic type of list object in the C# language. An Array is basically a list of objects of any type, with a given size. The size of the Array defines how many objects it can store. When the Array size is declared in your C# code, the size remains fixed as you use the Array. In other words, if you declare the Array to hold 5 integer numbers, the .NET runtime engine will allocate 5 Array placeholders in memory, ready to be populated with numbers. You cannot put 6 numbers into that Array, it can only hold up to 5. You can however put less than 5 if you so desire. Keep this characteristic about Arrays in mind when you are programming.
Each item stored in an Array is held at a specific position called the index. Arrays always start at position zero 0 and go upwards, 1, 2, 3… Here is what an Array of numbers looks like to the C# runtime:
Index Object
—— ——-
0 33
1 505
2 10
3 4202
Create a new website using Visual Web Developer and call the project Lab3. Go to the Design view for the file Default.aspx. Drag a new Button onto the form and change the Text property to say “Array of integers”. Drag a new TextBox to the right of the Button control. For the TextBox control, change the TextMode property to “MultiLine”. Double click the Button to create the event handler method and put the following code for the button:
int[] myarray = new int[4]; myarray[0] = 33; myarray[1] = 505; myarray[2] = 10; myarray[3] = 4202; for (int i = 0; i < myarray.Length; i++) { TextBox1.Text += myarray[i].ToString() + "\r\n"; }
Notice that we have declared an Array on the line int[] myarray = new int[4]; To declare an Array you have to specify what kind of data type you will stored in the array, in our case this is type int. Then you must put the square brackets [], this tells C# that you are declaring an Array. Following that we specify the name of our Array which is myarray. On the right side of the equals sign we have to put the “new” keyword and then specify the size of the Array by putting a number inside the square brackets; in this case the Array can hold up to 4 integers.
To load items into the Array, we have to use the Array name “myarray” followed by square brackets and then use the index number inside the square brackets. Then we can assign a number using the assignment operator =. The expression myarray[0] = 33; loads the number 33 into the first position of the array.
To read items from the Array, we also use the Array name and the index number with exactly the same syntax. If we wanted to get the number that is stored in the second position of the Array we would use the syntax myarray[1] in an expression. Note that in the code example above I am dynamically reading the items in the Array by using the expression myarray[i]. Since the variable i increments each time the loop iterates, this is dynamically changing which position in the Array is accessed each time. Notice that the loop starts at zero, since Arrays always start as position zero.
You can pretty much store any type of C# variable in an Array. We have seen how to store numbers in an Array. Let’s take a moment to experiment with an Array of strings. Go to the Design view of the form Default.aspx. Put your cursor to the right of the TextBox control and press Enter to create a new line. Drag a new button onto the form and change the Text to “Array of string”. Drag a new TextBox to the right of the button and change the TextMode to MultiLine. Double click the button to Create the event handler. In the event handler put the following:
string[] myarray = new string[3]; myarray[0] = "my first string"; myarray[1] = "my second string"; myarray[2] = "my last string"; for (int i = 0; i < myarray.Length; i++) { TextBox2.Text += myarray[i] + "\r\n"; }
The syntax is almost identical to the prior example when we were working with an Array of numbers. The only difference here is that in the for loop I didn’t have to call the ToString() method with the Array since the contents of the Array are already strings, therefore they don’t need to be converted in order to be displayed on the web page. This is in contrast to when we were storing numbers in the array.