2.6 Arrays

type [*]
+ array-name = 
[
  new type [ dimension+ ][*]*; |
  { value1, value2, ... };
]

Note that [*] is the set: [ ] [,] [,,] ...

Arrays allow a group of elements of a particular type to be stored in a contiguous block of memory. An array is specified by placing square brackets after the element type. For example:

char[ ] vowels = new char[ ] {'a','e','i','o','u'};
Console.WriteLine(vowels [1]); // Prints "e"

This prints "e" because array indexes start at 0. To support other languages, .NET can create arrays based on arbitrary start indexes, but all the libraries use zero-based indexing. Once an array has been created, its length cannot be changed. However, the System.Collection classes provide dynamically sized arrays, as well as other data structures, such as associative (key/value) arrays.

2.6.1 Multidimensional Arrays

Multidimensional arrays come in two varieties: rectangular and jagged. Rectangular arrays represent an n-dimensional block, while jagged arrays are arrays of arrays. In this example we make use of the for loop, which is explained in the statements section. The for loops here simply iterate through each item in the arrays.

// rectangular
int [,,] matrixR = new int [3, 4, 5]; // creates 1 big cube
// jagged
int [ ][ ][ ] matrixJ = new int [3][ ][ ];
for (int i = 0; i < 3; i++) {
   matrixJ[i] = new int [4][ ];
   for (int j = 0; j < 4; j++)
      matrixJ[i][j] = new int [5];
} 
// assign an element
matrixR [1,1,1] = matrixJ [1][1][1] = 7;

2.6.2 Local Field Array Declarations

For convenience, local and field declarations may omit the array type when assigning a known value, since the type is specified in the declaration anyway:

int[,] array = {{1,2},{3,4}};

2.6.3 Array Length and Rank

Arrays know their own length. For multidimensional array methods, the array's GetLength method returns the number of elements for a given dimension, which is from 0 (the outermost) to the array's rank-1 (the innermost).

// single dimensional
for(int i = 0; i < vowels.Length; i++);
// multi-dimensional
for(int i = 0; i < matrixR.GetLength(2); i++);

2.6.4 Bounds Checking

All array indexing is bounds-checked by the runtime, with IndexOutOfRangeException thrown for invalid indices. Like Java, this prevents program faults and debugging difficulties while enabling code to execute with security restrictions.

Generally, the performance hit from bounds-checking is minor, and the JIT (Just-in-Time compiler) can perform optimizations, such as determining each array index is safe before entering a loop, thus avoiding a check made for each iteration. In addition, C# provides "unsafe" code that can explicitly bypass bounds-checking (see Section 4.8 in Chapter 4).

2.6.5 Array Conversions

Arrays of reference types may be converted to other arrays, using the same logic you would apply to its element type (this is called array covariance). All arrays implement System.Array, which provides methods to generically get and set elements regardless of the array type.



    Part II: Programming with the .NET Framework
    Part IV: API Quick Reference
     
    ASPTreeView.com
     
    Evaluation has Ш¶ДЅexpired.
    Info...