Using Arrays in Microsoft® Visual C#® 2013
- By John Sharp
After completing this chapter, you will be able to:
Declaring and creating an array
Populating and using an array
Accessing an individual array element
Iterating through an array
You have already seen how to create and use variables of many different types. However, all the examples of variables you have seen so far have one thing in common—they hold information about a single item (an int, a float, a Circle, a Date, and so on). What happens if you need to manipulate a set of items? One solution is to create a variable for each item in the set, but this leads to a number of further questions: How many variables do you need? How should you name them? If you need to perform the same operation on each item in the set (such as increment each variable in a set of integers), how would you avoid very repetitive code? This solution assumes that you know, when you write the program, how many items you will need, but how often is this the case? For example, if you are writing an application that reads and processes records from a database, how many records are in the database, and how likely is this number to change?
Arrays provide a mechanism that helps to solve these problems.
Declaring and creating an array
An array is an unordered sequence of items. All the items in an array have the same type, unlike the fields in a structure or class, which can have different types. The items in an array live in a contiguous block of memory and are accessed by using an index, unlike fields in a structure or class, which are accessed by name.
Declaring array variables
You declare an array variable by specifying the name of the element type, followed by a pair of square brackets, followed by the variable name. The square brackets signify that the variable is an array. For example, to declare an array of int variables named pins (for holding a set of personal identification numbers) you can write the following:
int pins; // Personal Identification Numbers
You are not restricted to primitive types as array elements. You can also create arrays of structures, enumerations, and classes. For example, you can create an array of Date structures like this:
Creating an array instance
Arrays are reference types, regardless of the type of their elements. This means that an array variable refers to a contiguous block of memory holding the array elements on the heap, just as a class variable refers to an object on the heap. (For a description of values and references and the differences between the stack and the heap, see Chapter 8, “Understanding values and references”.) This rule applies regardless of the type of the data items in the array. Even if the array contains a value type such as int, the memory will still be allocated on the heap; this is the one case where value types are not allocated memory on the stack.
Remember that when you declare a class variable, memory is not allocated for the object until you create the instance by using new. Arrays follow the same pattern: when you declare an array variable, you do not declare its size and no memory is allocated (other than to hold the reference on the stack). The array is given memory only when the instance is created, and this is also the point at which you specify the size of the array.
To create an array instance, you use the new keyword followed by the element type, followed by the size of the array you’re creating between square brackets. Creating an array also initializes its elements by using the now familiar default values (0, null, or false, depending on whether the type is numeric, a reference, or a Boolean, respectively). For example, to create and initialize a new array of four integers for the pins variable declared earlier, you write this:
pins = new int;
The following graphic illustrates what happens when you declare an array, and later when you create an instance of the array:
Because the memory for the array instance is allocated dynamically, the size of the array does not have to be a constant; it can be calculated at run time, as shown in this example:
int size = int.Parse(Console.ReadLine()); int pins = new int[size];
You’re can also create an array whose size is 0. This might sound bizarre, but it’s useful for situations in which the size of the array is determined dynamically and could even be 0. An array of size 0 is not a null array; it is an array containing zero elements.