Arrays
Arrays are a special type, built into C#, that holds a group of other types. Arrays are a useful construct for organizing data. They provide matrix support and, even further, multidimensional support. As a collection, an array allows going beyond simple storage of data and provides fundamental discovery and manipulation of that data. Array methods are discussed in the next chapter. This chapter shows declaration and instantiation of single-dimension, multidimension, and jagged arrays.
For C++ Programmers
A C++ array is a pointer to a contiguous block of memory that is manipulated with pointers or indexes to store and retrieve data. C# arrays are objects with built-in functionality for operations such as sorting and determining length. C# array declarations place the brackets after the type rather than after the identifier.
For Java Programmers
Java allows the option of placing the brackets after the type or after the identifier. C# permits placement of the brackets only after the type.
Single-Dimension Arrays
Single-dimension arrays provide the capability to store and manipulate a list of items. Every element is of the same type. This is how such an array should be declared:
Type[] Array-Identifier [initializer] ;
The array identifier is any valid identifier. It should be meaningful for the purpose of the array. The optional initializer allocates memory for the array.
TIP
Once a C# array has been initialized, it can't change its size. If dynamic resizing of an array is needed, use one of the collection classes.
Here are some examples of single-dimensional array declarations:
// uninitialized declaration MyClass[] myArray; byte[] inputBuffer = new byte[2048]; // creates an array of 3 strings string[] countStrings = { "eins", "zwei", "drei" };
Arrays may be declared with no initialization. Remember, an array must be initialized before it's used. It may be initialized with an integer type value inside the brackets of the initializer, as the following example shows:
// creates an array of 3 strings string[] countStrings = new string[3] { "eins", "zwei", "drei" };
Another way to initialize an array is by leaving the space in the brackets of the initializer blank and then following the initializer brackets with an initializer list in braces. The array initializer list is a comma-separated list of values of the array type. The size of the array becomes the number of elements in the initializer list. If an integer value is added to the initializer brackets and there is an initializer list in the same array initializer, make sure that the integer value in the initializer brackets is greater than or equal to the number of elements in the initializer list. Take a look at this code sample:
// error string[] countStrings = new string[3] { "eins", "zwei", "drei", "vier" };
The initializer in this code fails with an error because the allocated size of the countStrings array is only 3, but the number of strings in the list is four. The number of strings in the list can't exceed the allocated size.
TIP
Don't specify a size when using an initializer list on an array. Later, it will be easier to add an item to the list and avoid the mistake of not incrementing the number.
N-Dimensional Arrays
Multidimensional arrays are similar in declaration and initialization to single-dimensional arrays, with a few exceptions. For every new dimension included, add a comma to brackets of the array declaration. During initialization, add an integer value to each of the dimensions specified in the declaration. Here are a couple examples:
long [ , ] determinant = new long[4, 4]; int [ , , ] stateSpace = new int[2, 5, 4]; bool [ , ] exclusiveOr = new bool[2, 2] { {false, true}, {true, false} };
Remember, the integer values in the initializer brackets are optional when including an initializer list.
Jagged Arrays
Jagged arrays allow creation of multidimensional arrays without the requirement of making every element of every dimension the same size. If an application has data that doesn't cover the entire range of possible values, this may be an option. It may also open the opportunity to save memory space. Here are some examples:
decimal[][] monthlyVariations = new decimal[12][]; monthlyVariations[(int)Months.Jan] = new decimal[31]; monthlyVariations[(int)Months.Feb] = new decimal[28]; . . . monthlyVariations[(int)Months.Dec] = new decimal[31];
For C++ Programmers
The enum type does not convert implicitly to an integer. It must be cast to an int to be used in arrays.
Using constant values from the months enum example, this shows how monthlyVariations has a different number of entries for each month. When creating a jagged array, first establish the size of the first dimension. Here, it's 12 to correspond to the months in the year. Once the first dimension is created, make each element of the second dimension any size needed. Continue for as many dimensions as required.
Array assignments are done by identifying the index of the array element to which a value will be assigned. Here are a few examples:
int[] month = new int[12]; month[(int)Months.Jan] = 31; double[,] affineTransform = new double[4, 4]; affineTransform[0, 2] = 1/Math.Sin(37.59); string[][] electoralMembers = new string[50][]; . . . electoralMembers[10] = new string[25]; electoralMembers[10][3] = "Smith";