In this lesson, we’re going to talk about storing values in arrays. The concept is not very difficult to understand if you think about “bucket” analogy we used to describe variables in previous lessons. An array is essentially a collection of values, under a single variable name, that all have the same value types, yet can hold distinct values. You can visualize an array as a row of buckets hanging from a broom-handle:


In this illustration, we “imprint” on the broom-handle the variable type that all the buckets must conform to, and initialize it with the number of distinct buckets we’re allowed to store values in. We can then reference the individual buckets in our code by referring to the index. Arrays are indexed starting from 0 and can hold as many individual buckets as you need (as long as itd doesn’t exceed the amount specified in when you initialize it). Here is how we would represent this particular array illustration:


Note that the array can hold only one data type (here, it is of type int), and the empty square brackets after the data type tells the compiler that this is, in fact, an array. The array must always be initialized with the “new” keyword, along with the data type and the size of the array (the amount of individual “buckets” specified in the brackets). Once you have values in each “bucket” you can reference them just as you would with any variable, except now you have to include the particular array element you want to use at any given moment:



Arrays are simple to create, however, they are extremely powerful. The simplest use of an array is to just group values that seem like they should belong together in some way. But the most common use for arrays is to iterate through them, performing some operation on each individual value. That leads us into loops, where arrays are often the star of the show. You’ll see this in action in later lessons.

Step 1: Create a New Project

Let’s start by creating a new ASP.NET project called “CS-ASP_021” and create the following Controls and programmatic IDs:

  1. TextBox1

  2. TextBox2

  3. TextBox3

  4. TextBox4

  5. TextBox5

  6. addButton

  7. retrieveButton

  8. resultLabel


Now, write this code in the addButton_Click event to create the array and then display the third element upon clicking the button:


Step 2: Arrays Indexes Start at [0]

Keeping in mind that the [2] index is the third value (since the index starts at [0]) we would expect the third TextBox entry to be output, via the resultLabel, after clicking the “Add” button:


Step 3: Using the Length Property to Count Array Size

Go back to the addButton_Click event and take a look at the variety of methods and properties available to the array. A common property is Length, which returns how many individual array elements there are, as an integer:


Let’s reference this property in code, so that we can see it displayed when clicking the “Add” button. We know ahead of time that this particular array has five elements:



You will see the use of the Length property in later lessons when iterating through arrays and needing to determine how many times the iteration has to execute (once for each array element, typically).

Step 4: Simplifying Array Syntax with Initializers

When initializing an array with individual values for each element, there is an easier way than you’ve already seen. You can write the initial values, for each index, in curly braces right where the variable is first declared, and assigned:


Now, combining this with what we learned about ViewState in the previous lesson, let’s add this array to the ViewState Dictionary and then retrieve values in the array by clicking on the retrieveButton_Click event:


Pay particular attention to the line that assigns the value held in ViewState[“MyValues”] to the string[] values variable. A cast, to string[], is needed here to make it work because the array actually gets stored in the dictionary as a generic object, which cannot implicitly be converted to a string. Now, when you run the application add the names to the ViewState by clicking on the “Add” button:


And, then display the values back, from the ViewState:


Step 5: Referencing an Out-Of-Range Index in an Array

Perhaps the most common error programmers face when working with arrays is referencing an index that is out of range. In this case, we have an array with five indexes (numbered 0 to 4) so if we were to attempt to read from an index that does not exist we would get an IndexOutOfRangeException:


This error also commonly occurs when trying to write a value to an index range that doesn’t exist. Here again, we only have five individual indexes numbered 0 to 4, yet we’re trying to access a non-existent sixth index at values[5]:



You may find it troublesome trying to remember which individual array value belongs to which particular index. This is made a bit more difficult owing to the fact that indexes start at [0]. There is a good computational reason for this being the case. It may be annoying, but soon you will get used to remembering that the highest index for your array is one less than the actual number of elements in your array.

Arrays are a very useful tool that can help you to store multiple values quickly and easily, then retrieve them in code when you need them. Keep them in mind as we’ll be using them frequently throughout this course. Great job!

Lessons in this Course